-1

I have looked at other examples but I am hopelessly lost. I have recorded a script in SAP. Now i just need to run in Excel as is. Script I recorded is below. How do I start and end the code?

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]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode =             "F00010"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode     "F00010"
session.findById("wnd[0]/tbar[1]/btn[17]").press
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[1]/usr/cntlALV_CONTAINER_1/shellcont/shell").selectedRows = "0"
session.findById("wnd[1]/usr/cntlALV_CONTAINER_1/shellcont/shell").doubleClickCurrentCell
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").text = "C:\TEMP\"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "BC.XLSX"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 7
session.findById("wnd[1]/tbar[0]/btn[0]").press
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
kim
  • 1
  • 1
  • You used the SAP GUI feature to record user actions, which generates a VB Script file (VBS). VB Script looks like VBA (VB for Microsoft Office applications) but may need to be adapted manually a little bit (like WScript which does not work directly in VBA but anyway this part of your code is not used). Here, I think you may just create a macro in Excel, paste your code and run the macro. Apart that, I don't really get your question. Can you clarify? – Sandra Rossi Mar 01 '20 at 17:13
  • Sounds simple...My problem is I don't get any of this. I Don't know how to adapt it. – kim Mar 03 '20 at 23:01

2 Answers2

0

The basic question is: What benefit will you get if you run this script in Excel. Your script does not include access to any Excel data. The result is identical, whether as a VB script or a VBA program. If you really want to run it as a VBA, test the following.

for example:

Sub Test()
Set SapGuiAuto  = GetObject("SAPGUI")
Set SAPapplication = SapGuiAuto.GetScriptingEngine
Set connection = SAPapplication.Children(0)
Set session    = connection.Children(0)
session.findById("wnd[0]").maximize
...
session.findById("wnd[1]/tbar[0]/btn[0]").press
End Sub

Regards, ScriptMan

ScriptMan
  • 1,580
  • 1
  • 9
  • 9
  • i have tested your example to run as VBA script, but it breaks at "Set Connection = SAPapplication.Children(0)". – kim Mar 07 '20 at 18:10
0

Try this:

Dim SapGuiAuto
Dim SetApp
Dim Connection
Dim Session

Set SapGuiAuto = GetObject("SAPGUI")
Set SetApp = SapGuiAuto.GetScriptingEngine
Set Connection = SetApp.Children(0)
Set Session = Connection.Children(0)

'''
Session.findById("wnd[0]").maximize
'''

kminito
  • 1
  • 2