Excel至SAP GUI

时间:2019-01-11 16:01:58

标签: excel vba sap-gui

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

我正在尝试通过ActiveX按钮运行VBA。由于某种原因,以下代码在尝试获取SAPGUI对象时失败。我已验证SAP已打开,并且在客户端和SAP系统上启用了脚本。有什么想法会导致失败吗?

1 个答案:

答案 0 :(得分:1)

我将首先对该程序进行以下扩展,然后对其进行进一步测试:

...
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

关于ScriptMan