Search code examples
vbasendkeys

VBA code to pass username & password


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?


Solution

  • 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