使用VBscript自动关闭excel弹出提示 [英] Automatically closing an excel popup prompt using VBscript

查看:179
本文介绍了使用VBscript自动关闭excel弹出提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个excel文件,应该通过Web查询访问远程监控服务器。由于文件中的数据必须定期刷新并保存,所以我已经写了一个.vbs脚本。它工作正常,但由于服务器使用基本的身份验证安全性,每次运行时无法关闭,excel会弹出一个弹出的Windows安全窗口,要求输入用户名和密码。有一个保存凭证的选项,但是仍然需要用户单击确定继续,但系统要求用户不进行任何交互。



Google搜索我发现这个堆栈交换帖子和其他类似的方法,所以我修改脚本为我的需要:

 设置wshShell = CreateObject(WScript.Shell)

Do
ret = wshShell.AppActivate(Windows Security)
如果ret = True然后
wshShell.SendKeys{enter}
退出Do
结束如果
WScript.Sleep 500
循环

脚本当窗口出现时触发,甚至在当时对焦点的任何窗口上注册输入按键,但它不能专注于弹出窗口本身。它在其他应用程序(如记事本或计算器)上工作得很好。这是某种特定的弹出窗口?如何修改脚本以专注于Excel弹出窗口?有其他更简单或更可靠的选择吗?



谢谢

解决方案

不幸的是,在弹出窗口和对话框窗口中,似乎并不总是很好地运行 AppActivate SendKeys >

有一个名为 <$ c的命令行实用程序$ c> nircmd ,但可以做你所需要的。这是一个非常好的工具,无论如何,你可能会找到各种其他用途。



下载它并将其放在与VBScript相同的文件夹中。或者,将其保存在您的 System32 / SysWow64 文件夹或包含在%path%环境变量中的任何其他文件夹中。然后在您的VBScript中包含以下语句:

 使用CreateObject(WScript.Shell)
.Runnircmd dlgWindows Securityclick ok
End with

dlg 命令具有以下参数:

  dlg [Process Name] [窗口标题] [动作] [参数] 

我们将离开 [Process名称] 空白(),只提供 [Window Title] 。请注意,这里的引号加倍,因为它们是一个VBS字符串文字。对于最后一个参数,您需要指定要推/按的按钮的控件ID。对于典型的对话框按钮(确定取消 等),您可以尝试使用 nircmd 的预定义控件ID: ok 取消 no 等等,否则,您需要使用像Spy ++这样的工具来确定按钮的控件ID,然后通过。


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.

这篇关于使用VBscript自动关闭excel弹出提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆