Can't call script more than once - vbscript

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 to Nothing 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}"

Related

VBScript loops in section of sub that does not contain looping code

Windows 10 Enterprise
Version 20H2
I am writing a VBScript that handles a print to PDF dialogue window.
The goal:
Detect that the printer dialogue window is open (using a loop that continuously checks)
If/when detected, handle the dialogue (send the filename, send the file path, click "print" to finish)
The problem: for some reason, the sub endlessly loops through an If statement. I don't understand why the code is looping through a section containing no loop code.
The code basically sends the same keys in a loop over and over again within the dialogue window, never completing the If statement.
As a small aside, I think the sendKeys command I use to close the dialogue is incorrect, but that should not cause an IF statement to loop.
Code (go to the section indicated by "#"'s to see where I am having issues):
Sub handlePrintDial()
iSeconds = 20
'================================================================================
' Set time variables
'================================================================================
tNow = now()
tFuture = DateAdd("s",iSeconds,tNow)
'================================================================================
' Set other objects
'================================================================================
Set WshShell = CreateObject("WScript.Shell")
sApp = "Save Print Output As"
sFileName = "This is a test-" & Year(now()) & Month(now()) & Day(now()) & Hour(now()) & Minute(now()) & Second(now())
'================================================================================
' Loop until window opens or time elapses
'================================================================================
Do Until Now() > tFuture
ret = WshShell.AppActivate(sApp)
If ret = True Then
Exit Do
End If
Loop
If ret <> True Then
MsgBox "Printer window not found. Please try again."
Exit Sub
End If
'================================================================================
' If printer window detected, handle window
'================================================================================
WScript.Sleep 500
ret = WshShell.AppActivate(sApp)
'########This is the top of the endless loop####################
If ret = True Then
ret = wshShell.AppActivate(sApp)
' Send filename
WScript.Sleep 2000
wshShell.sendKeys sFileName
WScript.Sleep 2000
' Send file path to save to
wshShell.sendKeys "{F4}"
WScript.Sleep 2000
wshShell.sendKeys "{BS}"
WScript.Sleep 2000
wshShell.sendKeys "^A"
WScript.Sleep 2000
wshShell.sendKeys "{del}"
WScript.Sleep 2000
wshShell.sendKeys "C:\Users\Username\Desktop\closeDialogue.vbs"
WScript.Sleep 2000
' "Click" print to complete the dialogue window
wshShell.sendKeys "{enter}"
WScript.Sleep 2000
wshShell.sendKeys "{enter}"
WScript.Sleep 2000
End If
WScript.Sleep 500
'########This is the bottom of the endless loop####################
WScript.Quit
End Sub
Call handlePrintDial()
Here is the window I am trying to handle:
After some experimentation, I discovered that the sendKeys "{enter}" commands were mysteriously causing the script to execute again (two simultaneous instances of the code running at once, per command line). When I removed Enter keys, the code ran normally without kicking off new instances of the script.
Summary: use sendKeys at your own peril. Specifically, it appears {enter} can cause the code to have unusual problems.

Hide taskbar with Taskbar Eliminator

With my tiny VBScript I'm trying to execute Taskbar Eliminator and run the key combination 'ALT + T' to hide the taskbar.
The program is being execute correctly but the key combination does not get recognized by the program.
Here's the code:
Set WshShell = WScript.CreateObject("WScript.Shell")
Dim strPath
Dim strKeyCombo
strPath = "C:\Taskbar_Eliminator.exe"
strKeyCombo = "%T"
WshShell.Run strPath, 1, true
WshShell.AppActivate "Taskbar_Eliminator.exe"
WshShell.SendKeys strKeyCombo
Maybe I'm just not seeing an obvious error but I'd be very glad if somebody could just point it out :)

Sendkeys "variable"

Is it possible to use the SendKeys the type the characters inside a variable.
for example I got characters stored in variable firstname = Regie
Here is my code.
On Error Resume Next
Dim objSysInfo, objUser
Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
firstname = objUser.givenName
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Notepad"
WScript.Sleep 400
WshShell.SendKeys "{& objUser.givenName}"
WScript.Sleep 250
Or is there any way to make this happen?
Sure you can, try this little script on the command line, it will print "test".
Set WshShell = WScript.CreateObject("WScript.Shell")
keys = "test"
WshShell.SendKeys keys

Set Proxy authentication for ie using Vbscript

I want to auto fill proxy authentication value of username and password for ie using vbscript.
After I added the proxy ip and port to Tools>Internet Option>Connection Tab>LAN Settings. I am prompted with the following dialog
Is there anyways using VBS OR VB to auto fill this ?
So, far i have got the code like so
'begin script
Option Explicit
Dim valUserIn
Dim objShell, RegLocate
Set objShell = WScript.CreateObject("WScript.Shell")
RegLocate = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"
objShell.RegWrite RegLocate,"0","REG_DWORD"
WScript.Sleep(1000)
valUserIn = Inputbox("Enter the Proxy server you want to use.","Proxy Server Required","proxygate.mydomain.com:8080")
if valUserIn = "" then
RegLocate = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"
objShell.RegWrite RegLocate,"0","REG_DWORD"
'MsgBox "No proxy mode"
else
RegLocate = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer"
objShell.RegWrite RegLocate,valUserIn,"REG_SZ"
RegLocate = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"
objShell.RegWrite RegLocate,"1","REG_DWORD"
'MsgBox "Proxy mode: " & valUserIn
end if
WScript.Quit
'end script
But this only set's the proxy ip and port.
Thanks in advance..
You can utilize the Sendkeys shell command which will type in and activate keyboard commands for you once it has allocated the correct screen. However, this is assuming you are running the script while it is open. If you are looking for something that prefills that data before hand. I'm afraid I cannot assist in that realm.
Dim WshShell, Success
Set WshShell = WScript.CreateObject("WScript.Shell")
'Wait for window to popup....
'if this doesn't activate it directly, try clicking on the window.
Do Until Success = True
Success = objShell.AppActivate("Windows Security")
Wscript.Sleep 1000
Loop
WScript.Sleep 500
WshShell.SendKeys "Username"
wscript.sleep 500 'allow program to have time to type it in.
WshShell.SendKeys "{TAB}" 'tab for password field
WshShell.SendKeys "Password"
wscript.sleep 500 'allow program to have time to type it in.
WshShell.SendKeys "%o" 'Alt + O for hitting "OK"
wscript.sleep 500

Scripting MMC with vbscript

I would like to add a snap in via vbscript and I have been having a problem getting the snap in to add to the console. It will be run in a Windows 7 environment. If someone could have a look see and direct me in the right direction I would be most grateful. Thanks.
<code>
'Elevated privileges start
'Start of UAC workaround code
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If WScript.Arguments.length =0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", Chr(34) & _
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
consoleName = "C:\Burnett.msc"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(consoleName) Then
Wscript.Echo "console already exists"
Else
On Error Resume Next
Set objMMC = CreateObject("MMC20.Application")
If err.Number <> 0 Then
Wscript.Echo "an error occurred. unable to create mmc console"
Wscript.Quit(0)
End If
objMMC.Show
Set objDoc = objMMC.Document
objDoc.snapins.add("Local Computer\Non-Administrators")
if err then
'Trap the error just after the statement where an error/exception can occur and handle it elegantly
msgbox("Snap-in Not found")
err.clear
end if
objDoc.ActiveView.StatusBarText = "Pane 1|Pane 2|Pane 3"
objMMC.UserControl = 1
objDoc.Name = consoleName
objDoc.Save()
End If
Set fso = Nothing
End If
</code>
"Local Computer\Non-Administrators" is just a system-supplied description for the particular configuration of a snap-in. In this case, the actual snap-in name is "Group Policy Object Editor". Thus to eliminate the error in the code change
objDoc.snapins.add("Local Computer\Non-Administrators")
to
objDoc.snapins.add("Group Policy Object Editor")
Unfortunately, this will only get you as far as MMC putting up a "Select Group Policy Object" dialog. You will then have to manually select the configuration you need using that dialog. As far as I can tell there is no way to supply Snapins.Add with the parameters to select the local non-admin users.
The code below will fully automate the process of setting up the snap-in. However, its reliance on SendKeys makes it extremely brittle. It worked on my system, but there's a good chance you'll need to modify the sequence of key strokes and/or the timing delays to make it work on your system. And once you get it working, there's no guarantee it will continue to do so as local conditions are mutable and can greatly effect the timing.
option explicit
if WScript.Arguments.Named.Exists("elevated") = false then
'Launch the script again with UAC permissions
CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
WScript.Quit
end if
Dim mmc : set mmc = WScript.CreateObject("MMC20.Application")
mmc.Show
mmc.UserControl = 1 'to keep MMC open
Dim oShell : set oShell = WScript.CreateObject("Wscript.Shell")
oShell.AppActivate "Console1"
WScript.Sleep 200
oShell.SendKeys "%f"
WScript.Sleep 200
oShell.SendKeys "m"
WScript.Sleep 400
oShell.SendKeys "group{TAB}{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{TAB}{TAB}{RIGHT}{TAB}Non{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{TAB}{ENTER}"
WScript.Sleep 1000
oShell.SendKeys "{TAB}{TAB}{TAB}{TAB}{ENTER}"

Resources