0
Private Sub CommandButton1_Click()
    On Error GoTo Err_NoSAP

    If Not IsObject(SAPGuiApp) Then
        Set SapGuiAuto = GetObject("SAPGUI")
        Set SAPGuiApp = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(Connection) Then
        Set Connection = SAPGuiApp.Children(0)
    End If
    If Not IsObject(SAP_session) Then
        Set SAP_session = Connection.Children(0)
    End If

    If IsObject(WScript) Then
        WScript.ConnectObject SAP_session, "on"
        WScript.ConnectObject SAPGuiApp, "on"
    End If

    If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP

    Set aw = SAP_session.ActiveWindow()
    aw.findById("wnd[0]").maximize

Err_NoSAP:
    MsgBox ("You don't have SAP open or " & Chr(13) & _
        "scripting has been disabled."), vbInformation, _
        "For Information..."
    Exit Sub

Err_TooManySAP:
    MsgBox ("You must only have one SAP session open. " & Chr(13) & _
        "Please close all other open SAP sessions."), vbInformation, _
        "For Information..."
    Exit Sub
End Sub

I am trying to run VBA via ActiveX button. For some reason that code shown below fails while trying to get the SAPGUI object. I have verified that SAP is open and I have scripting enabled on my client and SAP system. Any thoughts what could be causing the fail?

Dominique
  • 16,450
  • 15
  • 56
  • 112
Mwes
  • 23
  • 4
  • 2
    Fails *where* and *how*? – Tim Williams Jan 11 '19 at 18:20
  • I removed the `excel-vba` tag; it's description says not to use it because it's pending removal. I also removed the `vbscript` tag because your code is VBA, and they're not the same language any more than a car and a carrot are related because they both start with *car*. Can you [edit] to explain specifically what *fails* means, and where and how it does so? Thanks. – Ken White Jan 12 '19 at 00:56

1 Answers1

1

I would first do the following extension of the program and then test it further:

...
Set aw = SAP_session.ActiveWindow()
aw.findById("wnd[0]").maximize
'---------- new --------------------------------------
Exit Sub
'---------- new --------------------------------------

Err_NoSAP:
MsgBox ("You don't have SAP open or " & Chr(13) & _
    "scripting has been disabled."), vbInformation, _
    "For Information..."
    Exit Sub

Err_TooManySAP:
MsgBox ("You must only have one SAP session open. " & Chr(13) & _
    "Please close all other open SAP sessions."), vbInformation, _
    "For Information..."
Exit Sub

End Sub

Regards, ScriptMan

ScriptMan
  • 1,580
  • 1
  • 9
  • 9