简体   繁体   中英

Troubleshooting connection Excel VBA to SAP

I am trying to connect to an SAP GUI via Excel VBA in order to automate many of the data extractions from SAP. I have seen similar questions relating to connection errors at different locations of the connection process, however I have not come across any that directly address code at the "GetObject'SAPGUI'" portion of connection code.

Here is my code I have in tried thus far with the corresponding errors written in the comments of the code:

Sub SAP()

If Not IsObject(application) Then
   Set SapGuiAuto = GetObject("SAPGUI") 
   Set application = SapGuiAuto.GetScriptingEngine 'This outputs "compile error: Invalid use of property"
End if
...
End Sub

I figured that application is a reserved keyword in VBA and that was causing error by referring to incorrect objects. I then renamed to a unique variable name 'Sapplication', triggering an error at a different location:

Sub SAP()

If Not IsObject(sapplication) Then
   Set SapGuiAuto = GetObject("SAPGUI") 'This now outputs "Automation Error, Invalid syntax -2147221020"
   Set sapplication = SapGuiAuto.GetScriptingEngine 
End if
...
End Sub

Per the advice of an SAP forum, it was recommended that I replace GetObject("SAPGUI") with CreateObject("SAPGUI.Application") which prompts a new error:

Sub SAP()

If Not IsObject(sapplication) Then
   Set SapGuiAuto = CreateObject("SAPGUI.Application") 
   Set sapplication = SapGuiAuto.GetScriptingEngine 'Object doesn't support this method or property (438)
End if
...
End Sub

Not sure where I am going wrong with this code, the original code seems to be pretty standard as to what other people have obtained from the macro recorded within SAP. Any thoughts or suggestions on where I might be going wrong here?

UPDATE/EDIT: Upon a fresh SAP window and Excel boot, I'm able to successfully get further along in the code. However, I reach a new issue where there does not appear to be a connection object that is created (or at least instances related to that connection object that I can reference):


Set rotEntry =GetObject("SAPGUI")
Set sapplication = rotEntry.GetScriptingEngine 'I did confirm that use of 'application is conflicting per the SAP GUI documentation, so replaced with sapplication.
Set Connection = sApplication.children(0) 'Error: The enumerator of the collection cannot find an element with specified index


I have found that I sometimes need to try a slightly different login script like:

If Not IsObject(SapApp) Then
    On Error Resume Next
    Set SapGuiAuto = CreateObject("SAPGUI")
    If SapGuiAuto Is Nothing Then
        Err.Clear
        Set SapGuiAuto = GetObject("SAPGUISERVER")
    End If
    On Error GoTo 0
    
    Set SapApp = SapGuiAuto.GetScriptingEngine    'Object doesn't support this method or property (438)
End If

After a good amount of trial and error, I have found that opening through a shell command rather than having the GUI currently open is succesfull. I've posted the code that is currently working for me below:

Sub sap()


Dim sapgui
Dim applic
Dim connection
Dim session
Dim wshshell

Shell "C:\Program Files (x86)\SAP\FrontEnd\SapGui\saplogon.exe", vbNormalFocus 'OPEN SAP LOGON PAD 'edit to desired path
Set wshshell = CreateObject("WScript.shell")
Do Until wshshell.AppActivate("SAP Logon")
Application.Wait Now + TimeValue("0:00:02") 'set a delay timer to ensure the shell logon is ready

Loop

Set wshshell = Nothing
Set sapgui = GetObject("SAPGUI")
Set applic = sapgui.Getscriptingengine
Set connection = applic.OpenConnection("PA4 - Production North America ERP", True) 'edit this to reach the desired section within SAP

Set session = connection.Children(0)

'INSERT AUTOMATED MARCO HERE

End sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM