Thank you for Visiting my Blog

Saturday, 10 June 2017

Login to a Webpage using VBA

Hi Guys,

Prerequisite :  Before start using below code please make sure :
  1. Add references to Microsoft Internet Controls (shdocvw.dll)
  2.  Microsoft HTML object Library.
 Below is the VBA code Please modified as per the need:

Sub Login_Webpage()

' Variable Declaration

    

    Dim ObjectIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
    Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
    Dim htmlInput As MSHTML.HTMLInputElement
    Dim htmlColl As MSHTML.IHTMLElementCollection

    Set ObjectIE = New SHDocVw.InternetExplorer
     


    With ObjectIE
        .Navigate "
https://www.facebook.com" ' desired URL
        .Visible = 1
        Do While .ReadyState <> 4: DoEvents: Loop
            Application.Wait (Now + TimeValue("0:00:02"))
            
             'set user name and password
            Set htmlDoc = .Document
            Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
            Do While htmlDoc.ReadyState <> "complete": DoEvents: Loop
                For Each htmlInput In htmlColl
                    If htmlInput.Name = "email" Then  'This should be Name of Textbox ,which can be get using View source code of side
                        htmlInput.Value = "Username"
                   
                    End If
                   
                Next htmlInput
               
                 Do While htmlDoc.ReadyState <> "complete": DoEvents: Loop
                For Each htmlInput In htmlColl
                    If htmlInput.Name = "pass" Then
                        htmlInput.Value = "********"
                   
                    End If
                   
                Next htmlInput
               
                
                 'click login
                Set htmlDoc = .Document
                Set htmlColl = htmlDoc.getElementsByTagName("input")
                Do While htmlDoc.ReadyState <> "complete": DoEvents: Loop
                    For Each htmlInput In htmlColl
                        If Trim(htmlInput.Type) = "submit" Then  'This should be Type of Button
                            htmlInput.Click
                             Application.Wait (Now + TimeValue("0:00:05"))
                       
 

                            Exit For
                        End If
                    Next htmlInput
                End With
                
            End Sub

Thank you for Visiting ....

 

No comments:

Post a Comment