cancel
Showing results for 
Search instead for 
Did you mean: 

Using VBA in Excel with an already active session

djchaney3
Explorer
0 Kudos

Hello all,

     My company implemented SAP approximately 6 months ago and since this time I have been able to create several "assistance tolls" within Microsoft Excel that will logon to SAP, run certain TCODES as required, download the data into Excel and format it for ease of use for our supervisors.

     Here is my issue, if someone is already logged in to SAP and they run one of my tools it will error out due to the multiple logon screen that pops up. I have not been able to identify a work around, or code that will mark the "Continue with this logo, without ending any other logons in the system..." radio button.

 

Accepted Solutions (1)

Accepted Solutions (1)

djchaney3
Explorer
0 Kudos

I have discovered the issue and wanted to post it for others in case they have the same problem:

If Not IsObject(SAPguiApp) Then

     Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")

End If

If Not IsObject(connection) Then

     Set connection = SAPguiApp.OpenConnection("SAPERP", True)

End If

If Not IsObject(session) Then  

     Set session = connection.Children(0)

End If

'logon to SAP

session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "USERNAME_HERE"

session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "PW_HERE"

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

'process used if there are multiple sessions

If session.Children.Count Then   

     session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").Select   

     session.findById("wnd[1]/tbar[0]/btn[0]").press

End If

'proceed to the proper TCode screen using the desired layout (I used COOIS)

session.findById("wnd[0]").resizeWorkingPane 105, 31, False

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

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

session.findById("wnd[0]/tbar[1]/btn[17]").press session.findById("wnd[1]/usr/txtV-LOW").Text = "LAYOUT_TO_USE"

session.findById("wnd[1]/usr/txtENAME-LOW").Text = ""

session.findById("wnd[1]/tbar[0]/btn[8]").press

'populate the WC ranges within COOIS

session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_ARBPL-LOW").Text = wc1

session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_ARBPL-HIGH").Text = wc2

'populate the date ranges within COOIS  (firstDate and lastDate gathered by another macro)

session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_TEREN-LOW").Text = firstDate

session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_TEREN-HIGH").Text = lastDate session.findById("wnd[0]/tbar[1]/btn[8]").press

On Error Resume Next

'export to Excel

session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").pressToolbarButton "&NAVIGATION_PROFILE_TOOLBAR_EXPAND"

session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"

session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").selectContextMenuItem "&XXL"

'auto save the file to the desktop on certain versions (bypassed if the save window pops up

session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\" & usrId & "\Desktop\"

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

Answers (0)