I need to update several Excel workbooks via BEx Analyzer. I have a main script which calls scripts A and B.
Script A will fill out the SAP logon window that pops up every time data is refreshed inside the workbook.
Script B will enter the required date.
The problem I keep encountering is that the code works perfectly for the first workbook. Once the ExcelFileRefresh
function is called for the second (third, fourth, etc.) workbook, script A starts to fail. It will not activate the SAP Logon window, but instead send the keystrokes to another window, namely the folder containing the main script.
Strangely, if I then run script A manually by double clicking on it, it will work fine, and script B will do it's thing... Unfortunately I cant figure it out on my own.
List of unsuccessful attempts to fix this issue:
- I tried setting
WScript.Sleep
pauses. - I tried setting
SuccessSAPLogon
toNothing
at the end of script A. - I tried activating the Excel workbook before calling script A.
- I tried fusing script A and B into one file.
Main script
Filepath1 = "X:\ExcelBook1.xlsm"
Filepath2 = "X:\ExcelBook2.xlsm"
ExcelFileRefresh(Filepath1)
ExcelFileRefresh(Filepath2)
Function ExcelFileRefresh(Var)
Set Excel = CreateObject("Excel.Application")
Set objShell = CreateObject("WScript.Shell")
Excel.Visible = True
Excel.Workbooks.Open("C:\Program Files (x86)\Common Files\SAP Shared\BW\BExAnalyzer.xla")
Excel.Run ("BExAnalyzer.xla!SetStart")
Set Connection = Excel.Run("BExAnalyzer.xla!sapBEXgetConnection")
Excel.Run("BExAnalyzer.xla!sapBEXinitConnection")
Set ExcelBook = Excel.Workbooks.Open(Var, 0, False)
'--- Initiate Scripts ---'
Set objShell = CreateObject("WScript.Shell")
objShell.Run "ScriptA.vbs"
objShell.Run "ScriptB.vbs"
Excel.Application.Run ("MenuRefreshVariables")
Excel.Run "CommandButton1"
Excel.Run "retreiveData"
ExcelBook.Save
Excel.Quit
Set ExcelBook = Nothing
Set Excel = Nothing
Set objShell = Nothing
End Function
ScriptA
Set objShell = CreateObject("WScript.Shell")
SuccessSAPLogon = False
Do While SuccessSAPLogon = False
SuccessSAPLogon = objShell.AppActivate("SAP Logon")
Loop
objShell.SendKeys "{TAB 4}"
objShell.SendKeys "{RIGHT}"
objShell.SendKeys "{TAB}"
objShell.SendKeys "myWarehouse"
objShell.SendKeys "{TAB}"
objShell.SendKeys "BW"
objShell.SendKeys "{ENTER 2}"
ScriptB
Set objShell = CreateObject("WScript.Shell")
SuccessVariables = False
Do While SuccessVariables = False
SuccessVariables = objShell.AppActivate("Select Values")
Loop
objShell.SendKeys "{TAB 3}"
objShell.SendKeys "{DOWN}"
objShell.SendKeys "{TAB 14}"
DateA = DateSerial(Year(Date), Month(Date), Day(Date)-3)
objShell.SendKeys DateA
objShell.SendKeys "{ENTER}"