Skip to Content
avatar image
Former Member

Open and LOGON on SAP with Excel Macro

Hi,

i am trying to start SAP from excel VBA and input my information, like Log in, user name, password, etc.

To sum up, SAP is not open. I want to open it by clicking on the macro it will also input my user name and password into SAP.

Here is my coding.


When I click on the macro, its give me the following. (see image).


Any help?


Sub macro2()



Dim sapConn As Object

Set objshell = CreateObject("WScript.Shell")
Set objapp = objshell.Exec("C:\Program Files\SAP\FrontEnd\SAPgui\saplogon.exe")
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object

  sapConn.connection.ApplicationServer = "C:\Program Files\SAP\FrontEnd\SAPgui\saplogon.exe"
  sapConn.connection.client = "100" ' only read access
  sapConn.connection.user = Environ("USER")
  sapConn.connection.Password = "PASSWORD"
  sapConn.connection.System = ".VCM Global Production"
  sapConn.connection.Language = "EN"
  If sapConn.connection.logon(0, False) <> True Then 'Try Logon
  MsgBox "Cannot Log on to SAP"
  getSAPProjectHours = False
  Else
  
  sapConn.connection.Logoff
  End If
  Set sapConn = Nothing

End Sub
result.JPG (53.0 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Sep 30, 2014 at 04:14 PM

    Hello.

    Sub macro2()
    Dim sapConn As Object
    Set objshell = CreateObject("WScript.Shell")
    Set objapp = objshell.Exec(Environ("PROGRAMFILES") & "\SAP\FrontEnd\SAPgui\saplogon.exe")
    Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object
    sapConn.Connection.ApplicationServer = Environ("PROGRAMFILES") & "\SAP\FrontEnd\SAPgui\saplogon.exe"
    sapConn.Connection.client = "100" ' only read access
    sapConn.Connection.user = Environ("USERNAME")
    sapConn.Connection.Password = "PASSWORD"
    sapConn.Connection.System = ".VCM Global Production"
    sapConn.Connection.Language = "EN"
    If sapConn.Connection.logon(0, False) <> True Then 'Try Logon
    MsgBox "Cannot Log on to SAP"
    getSAPProjectHours = False
    Else: sapConn.Connection.Logoff
    End If
    Set sapConn = Nothing
    End Sub
    

    I have just changed mainly 'ENVIRON("USER")' as this not exist.

    Best regards,

    Holger

    Add comment
    10|10000 characters needed characters exceeded