How to execute SAP GUI Scripting in Excel VBA
- 자아실현/업무자동화
- 2022. 1. 11.
Even though source code made by SAP GUI Scripting (Recording) is .vbs file, it is not executed on excel VBA directly. If copy and past code from the .vbs file and run it, you can see below error message.
Compile Error
Invalid Use of property
Problematic part of the code is below. This is very beginning part of code which is from SAP Scripting. The error occurs because the variable 'application' is a reserved word in Excel. So it is not supposed to use as a variable in VBA.
<Code in problem> -> Error occurs
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
So if you change those part as below, you can execute rest of the code without any problem.
<Modifed code>
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)
<Example - entire code>
Sub test()
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
Session.findById("wnd[0]/tbar[0]/okcd").Text = "MMBE"
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/ctxtMS_MATNR-LOW").Text = "53924415"
Session.findById("wnd[0]/usr/ctxtMS_MATNR-LOW").caretPosition = 8
Session.findById("wnd[0]").sendVKey 8
End Sub