vba 传递用户名和密码的VBA代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24879914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:56:31  来源:igfitidea点击:

VBA code to pass username & password

vbasendkeys

提问by zeal

I'm trying to update some data into a SharePoint document and while trying this in local environment I do not face any issue.

我正在尝试将一些数据更新到 SharePoint 文档中,在本地环境中尝试此操作时,我没有遇到任何问题。

But when I try to do the same in virtual desktop, I couldn't make it. I'm being populated with Windows Alert to key-in username & password. I've tried using 'SendKeys' for this scenario, but it doesn't make sense.

但是当我尝试在虚拟桌面上做同样的事情时,我做不到。我正在填充 Windows Alert 以输入用户名和密码。我已经尝试在这种情况下使用“SendKeys”,但这没有意义。

....
SendKeys "abc\ATX123",5
SendKeys "Password1",5
SendKeys "{ENTER}",5
....

This snippet is just passing 'ENTER' without entering ID & Pwd. Can anyone suggest me some possible solution, please?

此代码段只是通过“ENTER”而不输入 ID 和密码。任何人都可以建议我一些可能的解决方案吗?

回答by zeal

Finally, I've found a way to achieve this requirement,,,,, bit indirect, but that was the only way I could find at the end.

最后,我找到了一种方法来实现这个要求,,,,,有点间接,但这是我最后找到的唯一方法。

What I did is simple - just created one windows scripted file 'Logon.vbs' to handle that popup screen and called the vbs in VBA.

我所做的很简单 - 只是创建了一个 Windows 脚本文件“Logon.vbs”来处理该弹出屏幕并在 VBA 中调用 vbs。

Refer to the sample below, if you're looking for something like this:

如果您正在寻找这样的东西,请参阅下面的示例:

Windows Script:

视窗脚本:

'Creating an script object
Set oWSH = WScript.CreateObject("WScript.Shell")

'Activating alert screen    
oWSH.AppActivate ("Windows Security")

'Passing the value UserName/UserID
oWSH.SendKeys "USERNAME"     'ensure to complete the username with apropriate domain e.g., abc/A123456

'Changing the focus to password textbox
oWSH.SendKeys "{TAB}"

'Passing the value password
oWSH.SendKeys "PASSWORD"

'Clicking enter to complete the screen
oWSH.SendKeys "{ENTER}"

'Releasing the script object
 Set oWSH = Nothing

VBA code to call the VBS script - Logon.vbs:

调用 VBS 脚本的 VBA 代码 - Logon.vbs:

Shell "WScript C:\Users\ABC\Desktop\Logon.vbs", vbNormalFocus

回答by Noldor130884

First of all you'll want to SendKeys "yourString", Boolean. As I know the SendKeyscommand wants a Trueor a Falseas second input. You could also try "tabbing" from a field to another:

首先,你会想要SendKeys "yourString", Boolean。据我所知,该SendKeys命令需要 aTrue或 aFalse作为第二个输入。您还可以尝试从一个字段“切换”到另一个字段:

SendKeys "abc\ATX123", True
SendKeys "{TAB}", True
SendKeys "Password1", True
SendKeys "{ENTER}", True

if the first one didn't get get written, maybe the focus is on another component of the userform. Try "tabbing" to find out if your "Username" textbox is yet activated as the UserForm appears. In case it isn't you could also try to set some focus

如果第一个没有被写入,可能重点是用户表单的另一个组件。尝试“tabbing”以查看您的“用户名”文本框是否在用户窗体出现时被激活。如果不是,您也可以尝试设置一些焦点