I have an excel file that is supposed to access a remote monitoring server through a web query. Since the data in the file has to be periodically refreshed and saved, I have written a .vbs script to do it. It works fine, but since the server uses basic authentification security, which cannot be turned off, each time it runs, excel throws a popup "Windows Security" window asking for the username and password. There is an option to "Save credentials", but it still requires for the user to click "OK" to proceed, but the system requires there to be no user interaction.
Googling around I found this stack exchange post and other similar approaches, so I modified the script for my needs:
Set wshShell = CreateObject("WScript.Shell")
Do
ret = wshShell.AppActivate("Windows Security")
If ret = True Then
wshShell.SendKeys "{enter}"
Exit Do
End If
WScript.Sleep 500
Loop
The script does trigger when the window appears and even registers an "enter" keypress on whatever window I have focus at that time, but it cannot focus on the popup window itself. It works perfectly fine on other applications such as "Notepad" or "calculator". Is this somehow specific to popup windows? How can I modify the script to focus on the "Excel" popup? Are there other, simpler or more reliable alternatives?
Thanks
Unfortunately, AppActivate and SendKeys don't seem to always work so well on popup and dialog windows.
There is a command-line utility called nircmd that can do what you need, however. It's a great tool to have anyway and you'll probably find various other uses for it.
Download it and throw it in the same folder as your VBScript. Or, save it in your System32/SysWow64 folder or any other folder included in your %path% environmental variable. Then include the following statements in your VBScript:
With CreateObject("WScript.Shell")
.Run "nircmd dlg """" ""Windows Security"" click ok"
End With
The dlg command has the following arguments:
dlg [Process Name] [Window Title] [Action] [Parameters]
We'll leave [Process Name] blank ("""") and just supply the [Window Title]. Note that the quotes here are doubled because they're within a VBS string literal. For the final argument, you need to specify the control ID of the button you want to push/click. For typical dialog buttons (OK, Cancel, Yes, No, etc), you can try using nircmd's predefined control IDs: ok, cancel, yes, no, etc. Otherwise, you'll need to use a tool like Spy++ to determine the button's control ID and pass that instead.
Related
I am calling directly VBA Macro from VBScript. The VBA Macro guides the user to Internet Explorer window pop-up, whereby the user is required to enter his login credentials.
What I am trying to achieve is to getElementbyName from the HTML of this pop-up and insert the values in the input field.
After getting the pop-up window, VBScript however does not continue working until I close respective excel file and pop-up window. I looked for the solution in the internet but did not find anything useful for that. Is there any workaround which will allow me to put the pop-up window on hold and let VBScript get element by ID in the pop up?
Here is my current VBScript Code -
xlsm_file_name = "webADI_template_Bankbuchungen_GL.xlsm"
'opening xlsm file and setting readonly to false
set xlobj = createobject("Excel.Application")
xlobj.Visible = TRUE
xlobj.Workbooks.Open("webADI_template_Bankbuchungen_GL.xlsm")
xlobj.Application.Run "'webADI_template_Bankbuchungen_GL.xlsm'!Sheet1.BneUploadCommon"
' from this moment VBScript does not go further until I manually close the excel file
Set objIE = WScript.CreateObject ("InternetExplorer.Application")
objIE.Document.getElementsByName("unamebean")(0).value = "niko"
xlobj.quit
EDIT:
I found out what causes VBScript to stop and not go further with next lines. Macro which is called in VBScript involves a long-lasting process of data accumulation with switching between functions and classes.
So at the moment of window pop-up, macro is not finished, as there are other father processes waiting behind for user's input.
I found in the forums, that to ensure that process happens on the background and the VBScript goes further, one may use WshShell Run - maybe a sensible solution would be to call Macro from Shell? - I did not manage to find the way to do that however.
Is there a way to let VBScript work further even if Macro which was called is not yet finished?
In Excel VBA, is it possible to change the window style of a shelled program?
Here's the situation in detail:
At the click of a command button, the user should be able to load a third-party scanning program. The software path and file are specified in a named cell called settings_scanningsoftware, as you can see below. However, I'm using the Shell program, which doesn't wait for the third-party software to load before continuing. If it did, my life would be easier. For that reason, I throw up a dialog box in Excel telling the user to wait for the software to load and then ready the item to be scanned before they click Ok. And, since I throw up a dialog box, I load the scanning program minimized so they can see the dialog box (instead of it being hidden behind the software window and they're wondering why nothing's happening). With me so far?
However, once they click Ok, I want Excel to give control to the scanning software and give it a regular-sized window. There's my problem: once the software is loaded minimized, I can't seem to change the window style to give it a regular window.
Here's my code:
On Error Resume Next
Dim ScannerShell As Integer
ScannerShell = Shell(Range("settings_scanningsoftware").Value, **vbMinimizedNoFocus**)
If (ScannerShell <> 0) Then
Dim MessageBoxValue As Integer
MessageBoxValue = MsgBox("When you're ready to scan the receipt, click Ok.", vbOKCancel, "Scan receipt")
If (MessageBoxValue = vbOK) Then
ScannerShell = Shell(Range("settings_scanningsoftware").Value, **vbNormal**)
AppActivate ScannerShell, False
SendKeys ("{TAB}{TAB}{TAB} ")
End If
End If
Notice I've got the vbMinimizedNoFocus bolded above so that the software loads minimized and the user can see the upcoming dialog box. Once they click Ok, though, the software's window should be normalized. But Excel won't do me that favour.
Is there a way to change the window style after it's loaded?
Never mind the SendKeys command. I put that in there to get the scanner to automatically scan once everything is in place (the key sequence is incomplete, but one problem at a time). I know SendKeys is not a great way of sending commands to a program, but I'll figure that out once I get this window style thing fixed.
Does anyone have any solutions for me? Thanks.
I'm trying to open a Google Chrome window, and continue to use that window to do many things. Which means I need to set it to a variable. Is there anyway to do this?
I have the following code to open the Google Chrome window and navigate to a URL, but I need to do more than that.
Sub Test()
Shell ("C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe -url https://google.ca")
End Sub
I want to type in my username and password, hit the submit button to log in, and do other things. Is there a way I can reference the chrome window like I could in Internet Explorer? (by using Set IE = ...)
Let us know exactly what you are trying to do in google chrome from excel.
If you are trying to send key commands you can try to use:
Shell ("C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe -url https://google.ca")
Application.Wait Now + 0.00003
Application.SendKeys "{Tab}", True
etc. etc. etc.
Hope this helps!
I am developing an application for automatic Excel to PDF generation. Every now and then (and without any apparent cause, the following popup comes up while the program runs:
Print driver host for 32bit applications stopped working.
and, shortly after, this one:
Printer setup
What, if anything, do I need to do here? My application is not overly complex. The error always (100%) happens on this line:
_application.ActiveWorkbook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF,
pdfTemplatePath, Excel.XlFixedFormatQuality.xlQualityStandard,
_, _, 1, pdfPrintAreaEnd);
where the variables in question are:
Excel := Microsoft.Office.Intertop.Excel
_application := Excel.Application
_ := System.Type.Missing
pdfPrintAreaEnd := int 6
The exception message is as follows:
System.Runtime.InteropServices.COMException (0x800A03EC):
Exception from HRESULT: 0x800A03EC
What is happening here? And, more importantly: Why is it only happening every one in a while?
Okay, so after some research into this matter, I encountered what I believe is one of the most confuzzling things about Excel automation I have encountered so far.
The error message is consistent with other cases of Excel usage that are both automated and not. The common denominator in all these cases is (buckle up!) that a network printer was set as default printer.
That's right - having a network printer as default, even when you print using a completely different printer or no printer at all (as in the case of exporting to PDF, which is not the same as printing to e.g. Adobe PDF Printer, right?) it will cause this error to sometimes on some machines pop up.
I changed the default printer to something internal, like Microsoft XPS Document Printer, tried again and I can now export hundreds and thousands of PDFs without a single occurrence of the error message.
I had a similar issue on my work laptop. The printer setup would pop-up when trying to execute ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:="\directory\MyFile".
The network printer was set as default. So I navigated to the printer in windows explorer. This prompted me to enter my network password. When I re-ran the code it worked splendidly.(You can find the location of the printer in Windows 10 by typing 'printer' in the windows search bar. Click on Printers And Scanners, navigate to your network printer and then click on manage -> Select Hardware Properties. The location will be displayed next to the image of the printer. In my case the printers location was specified in its name).
In summary, ensure you have access to the default printer. A simple test is trying to print something with your default printer.
If you don't mind changing your default printer follow #F.P solution above.
Someties it,s needed to close your workbook and reopen it.
In my case, when I opened an excel file, it prompted me to click the "Enable Editing" button in the picture below:
When I clicked on it, the excel file wants to open again and Excel splash screen shown again and I closed it immediately. And then I was not to able to save as a PDF both with "Print" button and "VB" code.
I'm trying to configure an automatic, Bluetooth-powered computer lock and unlock system. So far, I've got the lock script down, but I can't think of a script which would unlock the screen, as my comp is password protected. Any ideas?
You normally have two options when you're trying to script something. First, if the application is scriptable then you can issue applescript commands directly to the application. If it's not scriptable then your only other option is with GUI scripting where you simulate pressing buttons and typing keystrokes like if you were sitting in front of the computer doing it yourself. You use System Events commands to do this.
In your case I think you need the second option. In general when you gui script the application has to be frontmost. Since the "unlock screen" is probably frontmost at that moment you have a chance this will work. However I have not tried it so you'll need to do some testing to see if it's possible. Just do some searching for gui scripting examples and give it a try.
I would imagine your script will look something like the following. This assumes the cursor automatically is placed in the password section of the window so that the "keystroke" command will actually be typing in the proper place... otherwise you need to figure a way to put the cursor there before you type anything.
tell application "System Events"
keystroke "unlock password"
delay .5
click button "OK" of window 1
end tell