Amelie Peter
Amelie Peter

Reputation: 103

Authentication from excel VBA

I am trying some internet explorer automation in excel VBA. When user press the button , he is taken to gmail, where he has to authenticate himself. After authentication, he is allowed to post data to a web service. Here is my code

Dim AppUrl As String
AppUrl = "https://mail.google.com"
Call NavigateToURL(AppUrl)

Public Sub NavigateToURL(argURL)
Dim LoginURL As String
Dim ServiceUrl As String
Dim objIE As Object
Dim URl As String

LoginURL = "https://mail.google.com/mail/u/0/#inbox"


Set objIE = CreateObject("InternetExplorer.Application")

With objIE
.Visible = True
.Silent = False
.Navigate argURL
While IE.Busy
DoEvents
Wend
Do
    If objIE.LocationURL()= LoginURL Then
        MsgBox("Successfully Authenticated")
        Call requestPost 'to post excel data to a webservice

     Exit Do
    Else 
        MsgBox("Authentication failed")

    End If
Loop While objIE.LocationURL() <> (LoginURL)

End With

End Sub

But the problem I face is as soon as https://mail.google.com loads completely, the if condition is checked and displays the message authentication failed. I want the program to wait till https://mail.google.com/mail/u/0/#inbox loads. Is there a way to do that?

Upvotes: 1

Views: 910

Answers (1)

Jeanno
Jeanno

Reputation: 2859

Instead of using

While IE.Busy
DoEvents
Wend

try Application.Wait Now + TimeSerial(0, 0, 4) instead

Upvotes: 1

Related Questions