VBA code to pass username & password

梦想与她 提交于 2019-12-01 13:26:49

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

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

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