cancel
Showing results for 
Search instead for 
Did you mean: 

Excel VBA script cant detect my SAP Session

07-04-2019 11:14 AM
5651 views 3 comments
0 Likes
SAP Managed Tags
Subscribe

Here is my code on VBA, i followed guides from online but couldnt make anything out of it. Please help thanks!

Error came from first line and return me to Err_NoSAP: sap not opened / script disabled.

I am trying to run tcode ME21N and putting in some values. Hoping it will run, i will then change it to do a loop from excel data. But im stuck here, any idea what is the cause? My system is ERP(1)300

Private Sub CommandButton1_Click()


On Error GoTo Err_NoSAP


If Not IsObject(SAPGuiApp) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set SAPGuiApp = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = SAPGuiApp.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 SAPGuiApp, "on"
End If


If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP


Set aw = SAP_session.ActiveWindow()
aw.findById("wnd[0]").maximize


On Error GoTo Err_Description
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nme21n"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB0:SAPLMEGUI:0030/subSUB1:SAPLMEGUI:1105/cmbMEPO_TOPLINE-BSART").Key = "ZOS4"
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB0:SAPLMEGUI:0030/subSUB1:SAPLMEGUI:1105/ctxtMEPO_TOPLINE-SUPERFIELD").Text = "4000006"
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-EMATN[4,0]").Text = "5L3773:AA"
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/txtMEPO1211-MENGE[6,0]").Text = "2"
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-NAME1[15,0]").Text = "2S98"
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-LGOBE[16,0]").Text = "BORD"
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-LGOBE[16,0]").SetFocus
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0016/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/ctxtMEPO1211-LGOBE[16,0]").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/chkMEPO1211-UMSON[24,0]").Selected = True
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB2:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1211/tblSAPLMEGUITC_1211/chkMEPO1211-UMSON[24,0]").SetFocus
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT6/ssubTABSTRIPCONTROL1SUB:SAPLMEGUI:1313/ctxtMEPO1313-BWTAR").Text = "CATNEW"
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0019/subSUB3:SAPLMEVIEWS:1100/subSUB2:SAPLMEVIEWS:1200/subSUB1:SAPLMEGUI:1301/subSUB2:SAPLMEGUI:1303/tabsITEM_DETAIL/tabpTABIDT6/ssubTABSTRIPCONTROL1SUB:SAPLMEGUI:1313/ctxtMEPO1313-BWTAR").caretPosition = 6
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[12]").press
session.findById("wnd[1]/usr/btnSPOP-OPTION2").press
Exit Sub


Err_Description:
    MsgBox ("The program has generated an error;" & Chr(13) & _
    "the reason for this error is unknown."), vbInformation, _
    "For Information..."
        Exit Sub


Err_NoSAP:
MsgBox ("You don't have SAP open or " & Chr(13) & _
"scripting has been disabled."), vbInformation, _
"For Information..."
        Exit Sub


Err_TooManySAP:
MsgBox ("You must only have one SAP session open. " & Chr(13) & _
        "Please close all other open SAP sessions."), vbInformation, _
        "For Information..."
         Exit Sub


End Sub
0 Likes

Accepted Solutions (0)

Answers (1)

Answers (1)

Stefan-Schnell
Active Contributor
0 Likes

Hello Wang,

welcome in the SAP Community. I tried this part of your code - a little bit reduced.

Sub Test()

  On Error GoTo Err_NoSAP
  Set SapGuiAuto = GetObject("SAPGUI")
  Set SAPGuiApp = SapGuiAuto.GetScriptingEngine
  Set Connection = SAPGuiApp.Children(0)
  Set session = Connection.Children(0)

Err_NoSAP:
  MsgBox ("You don't have SAP open or " & Chr(13) & _
    "scripting has been disabled."), vbInformation, _
    "For Information..."
  Exit Sub

End Sub

Be sure that your SAP Logon is running, SAP GUI Scripting is installed and enabled. If one session is open you should reach the end of the sub routine without getting an error message.

Best regards
Stefan

former_member715313
Discoverer
0 Likes

If (Connection.Children.Count > 1) Then GoTo Err_TooManySAP Else GoTo continue

Err_TooManySAP: Shell ("TASKKILL /F /IM saplogon.exe /T")

GoTo ConnectionChildren

continue:

Set aw = sap_session.ActiveWindow() 'aw.findById("wnd[0]").Maximize

former_member715313
Discoverer
0 Likes

Hello, I need help with a script, I'm trying to verify that the open sap sessions are closed before but I can't get the code. You could help me

Best regards