1
votes

I want to edit and launch SAP macros from either the VBA editor in Excel or from a Windows Form Application.

I cannot get my code to "connect" to the current SAP session when the script is used in a compiler. The only time it works is when I open the original VBScript that the macro recorder creates.

The way SAP is set up on my system we are automatically logged in to SAP Logon and launch a new sap GUI window via a web portal saplogon.com/irj/portal, I don't need to physically log on before I interact with the GUI.

When I copy the macro script verbatim I get runtime errors:

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/subSA_0100_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2410/subSA_2410_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2510/ctxtKUWEV-KUNNR").text = "I033"
session.findById("wnd[0]/usr/subSA_0100_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2410/subSA_2410_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2510/ctxtKUWEV-KUNNR").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]/usr/lbl[12,3]").setFocus
session.findById("wnd[1]/usr/lbl[12,3]").caretPosition = 16
session.findById("wnd[1]").sendVKey 2
session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").setCurrentCell 1,"MAKTX2"
session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").doubleClickCurrentCell
session.findById("wnd[1]/tbar[0]/btn[0]").press`

The way I have been trying to go about this is to add the sapfewselib reference into the project and then declare the SAP GUI object.

All the examples I can find are on how to log on to a new SAP session. I can't understand why the code will execute when a notepad is saved as a VBScript file but not when attached to a button in a Windows Form Application or an Excel Spreadsheet even though I have added the reference.

1

1 Answers

1
votes

So I still can't find an example in c# which doesn't include log in but for the VBA editor in Excel the following works, but note in Visual Studio 'set' and 'let' keywords have been retired. Just in case anyone else has the same issue, this is what you need to assign to a button:

    Sub Button1_Click()

    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

//Then your script to control gui below this://
    Session.findById("wnd[0]/usr/subSA_0100_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2410/subSA_2410_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2510/ctxtKUWEV-KUNNR").Text = "I033"
    Session.findById("wnd[0]/usr/subSA_0100_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2410/subSA_2410_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2510/ctxtKUWEV-KUNNR").caretPosition = 4
    Session.findById("wnd[0]").sendVKey 0
    Session.findById("wnd[1]/usr/lbl[12,3]").SetFocus
    Session.findById("wnd[1]/usr/lbl[12,3]").caretPosition = 16
    Session.findById("wnd[1]").sendVKey 2
    Session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").setCurrentCell 1, "MAKTX2"
    Session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").doubleClickCurrentCell
    Session.findById("wnd[1]/tbar[0]/btn[0]").press

    End Sub