Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

sap gui - VBA opening new session in SAP GUI and execute transaction in it

Good day,

What would be the VBA code to open new session in SAP GUI (assuming user is logged into SAP already) and run transaction in that new session.

I would appreciate help.

question from:https://stackoverflow.com/questions/65833108/vba-opening-new-session-in-sap-gui-and-execute-transaction-in-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is the template I use, you just need to add activex button in excel then you can add /o before your transaction while you are recording it. But Dont forget most SAP system has session limits, thus I prefer using same session like most people.

mb52 => /omb52

Option Explicit

Public sapAuto As Object

Public WithEvents sapGUI As SAPFEWSELib.GuiApplication

Public sapCon As SAPFEWSELib.GuiConnection

Public WithEvents session As SAPFEWSELib.GuiSession

Dim WSHShell

Dim MyDate1 As Integer

Dim datemin As Date

Dim userID, userPassword, pathFolder As String

Private Sub CommandButton1_Click() 

connectSAP

mb52

killSAP

Application.CalculateFullRebuild

End Sub

Sub connectSAP() 'connect to SAP

On Error GoTo connectionError

Shell "C:Program Files (x86)SAPFrontEndSAPguisaplogon.exe", vbNormalFocus

Set WSHShell = CreateObject("WScript.Shell")

  Do Until WSHShell.AppActivate("SAP Logon ")

    Application.Wait Now + TimeValue("0:00:01")

  Loop

    Set sapAuto = GetObject("sapgui")

    Set sapGUI = sapAuto.GetScriptingEngine

    Set sapCon = sapGUI.Children(0)

    Set session = sapCon.Children(0)

    Exit Sub:

connectionError:

    MsgBox ("Connection Error")

    End

End Sub

 

Sub killSAP() ' kill sap connectio

    Set sapAuto = Nothing

    Set sapGUI = Nothing

    Set sapCon = Nothing

    Set session = Nothing

End Sub

 

Sub mb52() 'wrote/paste your script here


 
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...