cancel
Showing results for 
Search instead for 
Did you mean: 

How to save report using buton (VBA+Excel)

Former Member
0 Kudos

Hi Comunity,

does anybody know if it's posible save report by VBA macro using standard  button.

I tied record macro but unsuccessful.

If Not IsObject(application) Then

   Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.Children(0)

End If

If Not IsObject(session) Then

   Set session    = connection.Children(0)

End If

If IsObject(WScript) Then

   WScript.ConnectObject session,     "on"

   WScript.ConnectObject application, "on"

End If

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[1]/btn[43]").press

Recordered only press butoon

Accepted Solutions (0)

Answers (1)

Answers (1)

script_man
Active Contributor
0 Kudos

Hi Evgeniy,

yes you can. You just have to know that the script recorder can only record SAP GUI commands.

For example:

If Not IsObject(application) Then

   Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.Children(0)

End If

If Not IsObject(session) Then

   Set session    = connection.Children(0)

End If

If IsObject(WScript) Then

   WScript.ConnectObject session,     "on"

   WScript.ConnectObject application, "on"

End If

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[1]/btn[43]").press

'Here still follow SAP GUI commands.

session.findById("wnd[1]/usr/radRB_OTHERS").setFocus

session.findById("wnd[1]/usr/radRB_OTHERS").select

session.findById("wnd[1]/usr/cmbG_LISTBOX").key = "08"

session.findById("wnd[1]/tbar[0]/btn[0]").press

session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").select

session.findById("wnd[1]/tbar[0]/btn[0]").press

session.findById("wnd[1]/tbar[0]/btn[0]").press

'Here are specific Excel - commands that one can not record. This one needs to know.

Set xclapp = GetObject(,"Excel.Application")

xclapp.displayalerts = false

Set xclwbk = xclapp.workbooks.item("Worksheet in ALVXXL01 (1)")

xclapp.ActiveWorkbook.SaveAs("C:\tmp\myWorkbook.xlsx")

xclapp.ActiveWorkbook.close

Set xclwbk = Nothing

xclapp.displayalerts = true

set xclapp = Nothing

Regards,

ScriptMan

Former Member
0 Kudos

hi all,

what need to change in this code   if i need autosave in format *.mhtml ?

'Here still follow SAP GUI commands.

session.findById("wnd[1]/usr/radRB_OTHERS").setFocus

session.findById("wnd[1]/usr/radRB_OTHERS").select

'in this line i need to change to 02

session.findById("wnd[1]/usr/cmbG_LISTBOX").key = "08"

session.findById("wnd[1]/tbar[0]/btn[0]").press

session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").select

session.findById("wnd[1]/tbar[0]/btn[0]").press

session.findById("wnd[1]/tbar[0]/btn[0]").press

'Here are specific Excel - commands that one can not record. This one needs to know.

Set xclapp = GetObject(,"Excel.Application")

xclapp.displayalerts = false

'what need to change in these lines ?

Set xclwbk = xclapp.workbooks.item("Worksheet in ALVXXL01 (1)")

xclapp.ActiveWorkbook.SaveAs("C:\tmp\myWorkbook.xlsx")

xclapp.ActiveWorkbook.close

Set xclwbk = Nothing

xclapp.displayalerts = true

set xclapp = Nothing

if i change lines

session.findById("wnd[1]/usr/cmbG_LISTBOX").key = "02"

xclapp.ActiveWorkbook.SaveAs("C:\tmp\myWorkbook.mhtml")

when macro click button
session.findById("wnd[0]/tbar[1]/btn[43]").press

system open a window - "Save As"

thank you

script_man
Active Contributor
0 Kudos

Hi Yuriy,

you can try the following:

http://scn.sap.com/thread/1540783

Regards,

ScriptMan

Former Member
0 Kudos

Hi ScriptMan

I want to do same thing but it doesn't works. It stop on line "session.findById("wnd[0]/tbar[1]/btn[43]").press" and don't keep run. Please check my code below and tell me where do I'm wrong.

Public Sub Buscar_ZGLPM051()

'Login no SAP
Set SapGuiAuto = GetObject("SAPGUI")  'Obtenha o objeto SAP GUI Scripting
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Obter o SAP GUI atualmente em execução
Set SAPCon = SAPApp.Children(0) 'Obter o primeiro sistema que está conectado
Set session = SAPCon.Children(0) 'Obter a primeira sessão (janela) nessa conexão

'inicia seção ZGLPM051
session.StartTransaction "ZGLPM051"
session.findById("wnd[0]").maximize
On Error Resume Next
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").unselectAll
      If Err.Number <> 0 Then
End If

'execução dos comandos "script" gerado pelo SAP
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "F00039"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "F00018"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").topNode = "Favo"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00018"
session.findById("wnd[0]/tbar[1]/btn[17]").press
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[33]").press
session.findById("wnd[1]/usr/lbl[1,19]").SetFocus
session.findById("wnd[1]/usr/lbl[1,19]").caretPosition = 7
session.findById("wnd[1]").sendVKey 2
session.findById("wnd[0]/tbar[1]/btn[43]").press

'Here still follow SAP GUI commands.
session.findById("wnd[1]/usr/radRB_OTHERS").SetFocus
session.findById("wnd[1]/usr/radRB_OTHERS").Select
session.findById("wnd[1]/usr/cmbG_LISTBOX").Key = "08"
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press

'Here are specific Excel - commands that one can not record. This one needs to know.
Set xclapp = GetObject(, "Excel.Application")
xclapp.DisplayAlerts = False
Set xclwbk = xclapp.Workbooks.Item("Worksheet in ALVXXL01 (1)")
xclapp.ActiveWorkbook.SaveAs ("C:\tmp\myWorkbook.xlsx")
xclapp.ActiveWorkbook.Close
Set xclwbk = Nothing
xclapp.DisplayAlerts = True
Set xclapp = Nothing

Range("A2").Select

End Sub

Regards,

Camilo