Automatically closing an excel popup prompt using

2019-01-29 07:25发布

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

1条回答
成全新的幸福
2楼-- · 2019-01-29 07:43

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.

查看更多
登录 后发表回答