-1

While running my VBA code it opens all the correct functions, but when it comes to utilizing the export function - the drop down menu isn't available when pressing export button with VBA, while the drop down menu appears when clicking export button with mouse.

Export button example

Since it's unable to select the context menu items - given the dropdown does not appear- it gives the following error:

Run-time error '619': The control could not be found by id.

Is there a way to have the context menu appear for the export button ?

Sub SAPconn()

Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", 4
Set WshShell = CreateObject("WScript.Shell")
Do Until WshShell.AppActivate("SAP Logon ")
    Application.Wait Now + TimeValue("0:00:01")
Loop 'keeps looping the wait time until SAP is successfully opened

Set WshShell = Nothing

   Set SapGuiAuto = GetObject("SAPGUI") 'open SAP 
   Set app = SapGuiAuto.GetScriptingEngine 'use the VBScripting ability in SAP
      Set connection = app.Openconnection("Production", True) 'open the specific SAP connection
         Set session = connection.Children(0) 

ThisWorkbook.Activate 'ensure we are getting the information from this specific workbook for the next portion of code

With session
.findById("wnd[0]/usr/txtRSYST-BNAME").Text = Range("c2").Formula 'username
.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = Range("c3").Value 'password
.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN" 'language

.findById("wnd[0]").maximize
.findById("wnd[0]").sendVKey 0 'ENTER to log in 

end with
  
On Error GoTo 0
With session

.findById("wnd[0]").maximize
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "Root"
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "0000000007"
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "0000000039"
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").expandNode "0000000040"
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "0000000041"
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").topNode = "F00007"
.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "0000000041"
.findById("wnd[0]/usr/cmbP_ZINPST").key = "F"
.findById("wnd[0]/usr/cmbP_ZINPST").setFocus
.findById("wnd[0]/tbar[1]/btn[8]").press
.findById("wnd[0]").maximize

.findById("wnd[0]").maximize

.findById("wnd[0]/usr/cntlCONT1/shellcont/shell").PressToolbarContextButton ("&MB_EXPORT")
.findById("wnd[0]/usr/cntlCONT1/shellcont/shell").selectContextMenuItem "&XXL"
.findById("wnd[0]").sendVKey 0
End With
End Sub

Exampe of what code does is shown in this video: video

I have enabled scripting in SAP GUI. I have activated trust access to VBA project object model in the excel macros trust settings.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • If people reproduce using the program `SALV_VERI002` which exists in all systems (maybe data is first to be generated via program `BCALV_GENERATE_ALV_T_T2`), if you record the export button you mention and you play the script, the methods `PressToolbarContextButton` and `selectContextMenuItem` of [`GuiGridView`](https://help.sap.com/docs/search?q=GuiGridView&product=sap_gui_for_windows) work as expected i.e. start a Windows dialog to select a file (NB: it may simplify your script to deselect the SAP GUI option "Show native Microsoft Windows dialogs"). – Sandra Rossi Mar 16 '23 at 14:09
  • At which line does the RTE occur? I guess it is either `findById("wnd[0]/usr/cntlCONT1/shellcont/shell").PressToolbarContextButton ("&MB_EXPORT")` or `findById("wnd[0]/usr/cntlCONT1/shellcont/shell").selectContextMenuItem "&XXL"`. Is that right? Or is it on the second `.findById("wnd[0]").maximize`? – Storax Mar 19 '23 at 06:57
  • @Storax `findById("wnd[0]/usr/cntlCONT1/shellcont/shell").PressToolbarContextButton ("&MB_EXPORT")` is where is is supposed to open the context menu. weirdly enough I got it to semi work by opening a dialog through application. `Application.Dialogs(xlDialogOpen).Show("export")` after I close the dialog box for excel, the SAP code works perfectly - opens the context menu and selects XXL file type. I am not sure why-- and I am not sure how to make it work without an unrelated dialog box. Maybe I could set SAP as the application rather than excel and try the same method. – Lizi Campbell Mar 20 '23 at 13:55
  • Maybe you need to change one setting in the [SAPGUI](https://blogs.sap.com/2014/03/26/new-scripting-option-in-sap-gui-for-windows-730-pl-8/). Otherwise you could also have a look at an IMO now obsolete [approach](https://stackoverflow.com/a/58779515/6600940) – Storax Mar 20 '23 at 14:44

2 Answers2

1

Thank you @storax for the help be determining that is might be a SAPGUI setting error which led me into looking into solutions for the toolbox selection.

because the context menu item wasn't open in order to be found.

 with session
    .findById("wnd[0]/usr/cntlCONT1/shellcont/shell").PressToolbarContextButton ("&MB_EXPORT")
    .findById("wnd[0]/usr/cntlCONT1/shellcont/shell").selectContextMenuItem "&XXL"
    Application.Wait Now + TimeValue("0:00:03")
    .findById("wnd[0]").sendVKey
    end with

The wait time gave the SAP gui system the time required to open.

0

You need to manually open the context menu

Try using

' session.findById("wnd[0]/usr/cntlCONT1/shellcont/shell").contextMenu ' 

to open the context menu before trying to select the context menu item.

This should make it so the context menu appears

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77