I have several macros in a workbook, one to extract data from SharePoint lists, another to sort and cleanse the data and save it as a CSV file and another to open a specified webpage in Internet Explorer then select several links to navigate to a page where the user can upload the CSV file. The below code checks if the user is already logged in (using the title of the page to determine if the user is logged in or not). It then navigates/clicks through 3 links to get to the desired page and clicks the button to open the screen to allow the user to then select which CSV file to upload (my understanding is that this part can not be automated ~ if it can please point me in the right direction).
Occasionally the code will work if I press F8 to step through each line (takes a long time due to looping through to enter the username and password). However if I play it directly then I get a 'Run-time error '424': Object required error and I cant seem to work out why this is happening? My understanding is that the code checks if the webpage has loaded successfully before each click on a link.
Can anyone see the issue and provide some guidance on how to resolve it please?
Regards.
Sub IE_Automation()
Dim i As Long
Dim ie As Object
Dim objElement As Object
Dim objCollection As Object
Dim doc As Object
Dim wb As Object
Dim sURL As String
sURL = "https://XXXX" 'URL to navigate to
Set ie = CreateObject("InternetExplorer.Application") 'Create Internet Explorer instance
ie.Visible = True 'True = IE is visible, False = Not visible on screen
ie.navigate sURL 'Navigate to the URL defined in variable sURL
Do
If ie.READYSTATE = 4 Then 'Check if Internet Exlporer is ready (web page loaded)
ie.Visible = True
Set doc = ie.document 'define doc as current IE webpage
'Title
'Debug.Print doc.title 'for testing purposes - displays the web page title in the Immediate window
If doc.title = "TSMaster - " Then 'TSMaster - is the title of the webpage if the user is already logged in
GoTo Line1 'If the user is already logged in then jump straight to Line1 as user does not need to log in
End If
Exit Do 'End of Do event checking if webpage has loaded successfully
Else
DoEvents
End If
Loop
Application.StatusBar = "Search form submission. Please wait..."
Set objCollection = ie.document.getElementsByTagName("input")
i = 0 'Loop through all objects until the username object is found
While i < objCollection.Length
If objCollection(i).Name = "os_username" Then 'Username field is called os_username
' Set text for search
objCollection(i).Value = "XXusernameXX" 'set the username field value
Else
End If
i = i + 1
Wend
i = 0 'Loop through all objects until the password object is found
While i < objCollection.Length
If objCollection(i).Name = "os_password" Then 'Password field is called os_password
' Set text for search
objCollection(i).Value = "XXpasswordXX" 'set the password field value
Else
If objCollection(i).Type = "submit" And _
objCollection(i).Name = "login" Then 'check if the object found is a submit button named login
' "Search" button is found
Set objElement = objCollection(i)
End If
End If
i = i + 1
Wend
objElement.Click ' click button to search
Do
'Debug.Print ie.READYSTATE
If ie.READYSTATE = 4 Then 'Check if Internet Explorer is ready (web page loaded)
GoTo Line1
Exit Do
Else
DoEvents
End If
Loop
Line1: 'Line 1 'Go To' for when the user is already logged in
Do
'Debug.Print ie.READYSTATE
If ie.READYSTATE = 4 Then 'Check if Internet Explorer is ready (web page loaded)
ie.document.getElementById("find_link").Click 'click Issues on top menu
Exit Do
Else
DoEvents
End If
Loop
Do
If ie.READYSTATE = 4 Then 'Check if Internet Exlporer is ready (web page loaded)
ie.document.getElementById("bulk_create_dd_link_lnk").Click 'click import Issues from CSV
Exit Do
Else
DoEvents
End If
Loop
Do
If ie.READYSTATE = 4 Then 'Check if Internet Exlporer is ready (web page loaded)
ie.document.getElementById("csvFile").Click 'click button to select file to use
Exit Do
Else
DoEvents
End If
Loop
End Sub
Bookmarks