cancel
Showing results for 
Search instead for 
Did you mean: 

Convert vbs2vba (Run SAP GUI Script as Macro in Excel)

Former Member
0 Kudos

Hello everybody,

I recorded the following simple script in SAP wich only runs the transaction /nse16. When I run this file in Windows it works.

SE16.vbs:

If Not IsObject(application) Then

   Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.Children(0)

End If

If Not IsObject(session) Then

   Set session    = connection.Children(0)

End If

If IsObject(WScript) Then

   WScript.ConnectObject session,     "on"

   WScript.ConnectObject application, "on"

End If

session.findById("wnd[0]").resizeWorkingPane 128,41,false

session.findById("wnd[0]/tbar[0]/okcd").text = "/nse16"

session.findById("wnd[0]").sendVKey 0

How can I run this script from Excel as a macro?

Sub SapMacro()

'

' Run Transaction SE16

'

'How to transform the vbs to an Excel Macro?

End Sub

I am allready logged in to the SAP-System an Scripting ist enabled in SAP.

Thanks!

Accepted Solutions (0)

Answers (1)

Answers (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Public User,

try this:

Sub SapMacro()

'

' Run Transaction SE16

'

'How to transform the vbs to an Excel Macro?

  Set SapGuiAuto = GetObject("SAPGUI")

  If Not IsObject(SapGuiAuto) Then

    Exit Sub

  End If

  Set App = SapGuiAuto.GetScriptingEngine

  If Not IsObject(App) Then

    Exit Sub

  End If

  Set Connection = App.Children(0)

  If Not IsObject(Connection) Then

    Exit Sub

  End If

  Set session = Connection.Children(0)

  If Not IsObject(session) Then

    Exit Sub

  End If

  session.findById("wnd[0]").resizeWorkingPane 128, 41, False

  session.findById("wnd[0]/tbar[0]/okcd").Text = "/nse16"

  session.findById("wnd[0]").sendVKey 0

End Sub

The variable name Application is not allowed in Office context, it is occupied from Office. The code is nearly the same as VBS, there is no great difference.

Let us know your results.

Cheers

Stefan