trouble using excel macro for access to sap

只谈情不闲聊 提交于 2019-12-24 10:54:25

问题


I am trying to make a macro in excel that will log me on to SAP, the code I have gives me a popup window confirming my information however when I submit the form it does not take me to a new SAP window.

Is there extra code I am missing or did i type something wrong? Additionally is there a way to automate all the popup boxes asking for confirmation on my information, as I want this code eventually to run at certain times of the day, and I might not be available to input any data.

Sub login1()
Dim sap As Object
Dim conn As Object

Set sap = CreateObject("SAP.Functions")
Set conn = sap.Connection
conn.System = "System Test Environment"
conn.client = "100"
conn.user = "user"
conn.Password = "password"
conn.Language = "EN"

If conn.logon(0, False) <> True Then
    MsgBox "Logon to the SAP system is not possible", vbOKOnly, "Comment"
    Else

End If
End Sub

回答1:


This Macro will never open a SAP Window - it will create an SAP-Object within VBA where you can work with SAP-RFC-Functions. (Reading Data from SAP, Writing Data into SAP)

In your version the SAP connection will be unaccessible after "End Sub". You have to declair the Object outside the sub.

This works silent (without dialog) for me:

Dim sap As Object

Public Function login1() As Boolean

  Set sap = CreateObject("SAP.Functions")

  sap.Connection.System = "System Test Environment"
  sap.Connection.client = "100"
  sap.Connection.user = "user"
  sap.Connection.Password = "password"
  sap.Connection.Language = "EN"

  If sap.Connection.logon(0, False) <> True Then
    sap.RemoveAll
    MsgBox "Logon to the SAP system is not possible", vbOKOnly, "Comment"
  Else
    login1 = true
  End If

End Function

Public Function SAPLogoff()
    On Error Resume Next
    sap.RemoveAll
    sap.Connection.logoff

    LoggedOn = False
    Set sap = Nothing
    'Set conn = Nothing
End Function



回答2:


Since you want to "open a new SAP Window" you have to make a different approach!

At First try to open the new instance of SAP from the DOS Commandline with "sapshcut":

C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe -system="System Test Environment" -client="100" -user="user" -password="password" -language="EN"

If your SystemName has no Spaces (and I belive so!) then you can write it also like:

C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe -system=SystemTestEnvironment -client=100 -user=user -password=password -language=EN

When this call works with your credentials, then you can transfer it to Excel like this:

Sub login1()

  Call Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe -system=SystemTestEnvironment -client=100 -user=user -password=password -language=EN",vbNormalFocus)

End Sub

You could also add a transaction by adding "-command=your_tcode" to the String.

If your have spaces in the parameters and you could only start it with -system="System Test Environment" from the Commanline, you will have to escape this in Excel with -system="""System Test Environment""" (!)



来源:https://stackoverflow.com/questions/47143866/trouble-using-excel-macro-for-access-to-sap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!