VBA code to pass username & password

北慕城南 提交于 2019-12-01 11:40:14

问题


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

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.

....
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?


回答1:


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.

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:

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



回答2:


First of all you'll want to SendKeys "yourString", Boolean. As I know the SendKeys command wants a True or a False as second input. You could also try "tabbing" from a field to another:

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



来源:https://stackoverflow.com/questions/24879914/vba-code-to-pass-username-password

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!