Skip to Content
Former Member
Nov 01, 2016 at 07:33 PM

help to convert vba to vbs


Hi experts.

Can you help me to convert this macro to vbs script.

This macro is to select the last sap session.

Option Explicit

Sub test()

  Dim SapGuiApp As Object

  Dim oConnection As Object

  Dim Session As Object

  Dim SAPCon As Object, SAPSesi As Object

  Dim SAPGUIAuto As Object, SAPApp As Object

  Dim NumOfRows As Long

  Dim Counter As Long

  Dim SAP As Object, SAPGUI As Object, SAPConnections As Object

  Dim cntConnection As Long, i As Long, SAPConnection As Object

  Dim Sessions As Object, cntSession As Long, j As Long

  Dim SessionExists As Boolean


  Application.ScreenUpdating = False

  NumOfRows = ActiveSheet.UsedRange.Rows.Count - 3

  SessionExists = False

  On Error Resume Next


  Set SAP = GetObject("SAPGUI")

  If Not IsObject(SAP) Then

    Exit Sub

  End If


  Set SAPGUI = SAP.GetScriptingEngine

  If Not IsObject(SAPGUI) Then

    Exit Sub

  End If

  Set SAPConnections = SAPGUI.Connections()

  If Not IsObject(SAPConnections) Then

    Exit Sub

  End If

  cntConnection = SAPConnections.Count()

  For i = 0 To cntConnection - 1

    Set SAPConnection = SAPGUI.Connections(CLng(i))

    If IsObject(SAPConnection) Then

      Set Sessions = SAPConnection.Sessions()

      If IsObject(Sessions) Then

        cntSession = Sessions.Count()

        For j = 0 To cntSession - 1

          Set Session = SAPConnection.Sessions(CLng(j))

          If IsObject(Session) Then

            SessionExists = True

          End If

        Next j

      End If

    End If

  Next i


  If SAPConnection Is Nothing Then

    MsgBox "Error.. no SAP session could be found"

    Exit Sub

  End If


  With Session
 .findById("wnd[0]/tbar[0]/okcd").Text = "/nfbl3n"

  End With

End Sub

credits to

Stefan Schnell

Former Member