I’m a VBA noob, this is my first post on StackOverflow, please be kind.
I have an Excel Macro / VBA script which exports a table from SAP as Excel file. It then activates this new excel file. Here is an excerpt from my code:
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectedRows = ""
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
session.findById("wnd[1]/tbar[0]/btn[0]").press
filname = "Reuse_" & Date & Hour(Now) & ".xlsx"
session.findById("wnd[1]/usr/ctxt[1]").Text = filname
session.findById("wnd[1]/usr/ctxt[1]").caretPosition = 7
session.findById("wnd[1]/tbar[0]/btn[0]").press
Windows(filname).Activate
When I run it with the debugger step-by-step in VBA, this works. But when I run it as a whole, it breaks off, with two popup windows showing up. Note that the exported excel file has not opened yet at this time (which I think is the very problem).
One VBA popup telling me ‘Runtime error ‘9’: Index out of range’.
And one Excel popup telling me ‘Excel cannot perform the desired action because a dialog box is open.’
If I click on OK on the Excel popup and on ‘Debugging’ on the VBA popup, two things happen simultanously:
- The last line in the code above is highlighted, indicating that VBA can’t find the exported file.
- The desired Excel file immediately opens (alas, too late).
I tried adding actions in SAP or Excel before the activation of the exported file. But the file didn’t open then, either (only in debugging mode). I also tried adding wait-time.
TheBlues is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1