Run time error 91 - IE Object - vba

The code works correctly but when I try to close IE I get the following error:
"Run Time Error 91"
Dim IE As Object
Dim shellWins As New ShellWindows
Dim IE_URL As String
Dim Row As Integer
Row = 2
For Each IE In shellWins
IE_URL = IE.LocationURL
If IE_URL <> vbNullString Then
Sheet1.Range("A" & Row) = IE_URL
row = Row + 1
End If
Next
Set shellWins = Nothing
Set IE = Nothing
IE.quit
NEW CODE:
Dim shellWins As New ShellWindows
Dim objIE As Object
Dim objIE_TabURL As String
Dim Rowcount As Long
Rowcount = 2
With objIE
Set objIE = CreateObject("InternetExplorer.Application")
For Each objIE In shellWins
objIE_TabURL = objIE.LocationURL
If objIE_TabURL <> vbNullString And InStr(objIE_TabURL,
"file://") = 0 Then
Rowcount = Rowcount + 1
sht2.Range("A" & Rowcount) = objIE_TabURL
End If
Next
objIE.Quit
End With
END NEW CODE
I try to read the url from tabs open in a IE session to export it in a sheet.
I'm so sorry but i'm a newbie in a VBA :-(

IE isn't actually set to an instance of Internet Explorer at any point, you're using it as an object variant in a loop. Once the loop finishes, the variables becomes out of scope, and therefore cannot be accessed any more in the way you would like.
If you want to close IE after retrieving the URL, change your code to:
For Each IE In shellWins
IE_URL = IE.LocationURL
If IE_URL <> vbNullString Then
Sheet1.Range("A" & Row) = IE_URL
row = Row + 1
IE.Quit '// Close IE here, while the variable is still in scope
End If
Next
Note there is no need to Set IE = Nothing at the end of your routine as VBA automatically manages memory for you.

Related

Macro to open multiple links in new tabs

I want my macro to open each link stored in a spreadsheet in a separate IE tab. I am successful with opening the first link, but for some reason on the second iteration of the loop I get:
Automation error.The interface is unknown
error.
I suspect the macro somehow loses IE object reference after first iteration, but I am not sure why.
Range is set OK.
Here is the code:
Sub OpenCodingForms()
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim CodingFormLinks As Range
Dim IE as InternetExplorerMedium
Set wb1 = Workbooks("New shortcut.xlsm")
Set ws1 = wb1.Worksheets("Data")
Set CodingFormLinks = ws1.Range("A2", Range("A2").End(xlDown))
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
ws1.Activate
For Each link In CodingFormLinks.Cells
IE.Navigate link, CLng(2049)
Next link
End Sub
I ran into this issue before and ended up just writing a routine to get the instance. You will need to add a reference to shell controls and automation.
you may have to adjust this to look for the sURL var in the beginning of the actual URL if there is redirection.
Sub OpenCodingForms()
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim CodingFormLinks As Range
Dim IE As InternetExplorerMedium
Set wb1 = Workbooks("New shortcut.xlsm")
Set ws1 = wb1.Worksheets("Data")
Set CodingFormLinks = ws1.Range("A2", Range("A2").End(xlDown))
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
ws1.Activate
Dim sUrl As String
For Each link In CodingFormLinks.Cells
sUrl = link.Value
IE.navigate sUrl, CLng(2048)
Set IE = GetWebPage(sUrl)
Next link
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Desc: The Function gets the Internet Explorer window that has the current
' URL from the sURL Parameter. The Function Timesout after 30 seconds
'Input parameters:
'String sURL - The URL to look for
'Output parameters:
'InternetExplorer ie - the Internet Explorer window holding the webpage
'Result: returns the Internet Explorer window holding the webpage
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetWebPage(sUrl As String) As InternetExplorer
Dim winShell As Shell
Dim dt As Date
'set the timeout period
dt = DateAdd("s", 300, DateTime.Now)
Dim IE As InternetExplorer
'loop until we timeout
Do While dt > DateTime.Now
Set winShell = New Shell
'loop through the windows and check the internet explorer windows
For Each IE In winShell.Windows
'check for the url
If IE.LocationURL = sUrl Then
'set the window visible
IE.Visible = True
IE.Silent = True
'set the return value
Set GetWebPage = IE
Do While IE.Busy
DoEvents
Loop
Set winShell = Nothing
Exit Do
End If
Next IE
Set winShell = Nothing
DoEvents
Loop
End Function

Error While using Macro to add google's first image link to excel

I'm using the below Code to input Google's first images link in B1
for certain values in A1.
Public Sub Test()
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim imgElements As IHTMLElementCollection
Dim imgElement As HTMLImg
Dim aElement As HTMLAnchorElement
Dim N As Integer, I As Integer
Dim Url As String, Url2 As String
Dim LastRow As Long
Dim m, sImageSearchString
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For I = 1 To LastRow
Url = "http://www.google.co.in/search?q=" & Cells(I, 1) & "&source=lnms&tbm=isch&sa=X&rnd=1"
Set IE = New InternetExplorer
With IE
.Visible = False
.Navigate Url 'sWebSiteURL
Do Until .readyState = 4: DoEvents: Loop
'Do Until IE.document.readyState = "Complete": DoEvents: Loop
Set HTMLdoc = .document
Set imgElements = HTMLdoc.getElementsByTagName("IMG")
N = 1
For Each imgElement In imgElements
If InStr(imgElement.src, sImageSearchString) Then
If imgElement.ParentNode.nodeName = "A" Then
Set aElement = imgElement.ParentNode
Url2 = imgElement.src
N = N + 1
End If
End If
Next
Cells(I, 2) = Url2
IE.Quit
Set IE = Nothing
End With
Next
End Sub
however I'm receiving the below error, can you please advise?
I'm using Windows 10, Excel 365
In VBA Menu - Tools - References - tick MS Internet Controls.
Or
Using Late Binding
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")

Web scraping with Excel and VBA

I am trying to extract the team names but i get the "Run time error 424 Object required" on this line
Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container")
, if anyone could point me in the right direction it would be nice.
Sub useClassnames()
Dim lists As IHTMLElementCollection
Dim anchorElements As IHTMLElementCollection
Dim ulElement As HTMLUListElement
Dim liElement As HTMLLIElement
Dim row As Long
Dim ie As InternetExplorer
Set ie = New InternetExplorer
With ie.navigate "https://www.unibet.ro/betting#filter/all/all/all/all/in-play"
.Visible = True
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With
Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container")
row = 1
For Each ulElement In lists
For Each liElement In ulElement.getElementsByClassName("KambiBC-event-participants")
Set anchorElements = liElement.getElementsByClassName("KambiBC-event-participants__name")
If anchorElements.Length > 0 Then
Cells(row, 1) = anchorElements.Item(0).innerText
row = row + 1
End If
Next liElement
Next ulElement
End Sub
Error 424 is an Object Required Error. What is Html in Set lists = html.getElementsByClassName("KambiBC-event-item__participants-container")?
What you need is
Set lists = ie.document.getElementsByClassName("KambiBC-event-item__participants-container")
Code that I used
Sub useClassnames()
Dim lists As IHTMLElementCollection
Dim anchorElements As IHTMLElementCollection
Dim ulElement As HTMLUListElement
Dim liElement As HTMLLIElement
Dim row As Long
Dim ie As InternetExplorer
Set ie = New InternetExplorer
With ie
.navigate "https://www.unibet.ro/betting#filter/all/all/all/all/in-play"
.Visible = True
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With
Set lists = ie.document.getElementsByClassName("KambiBC-event-item__participants-container")
row = 1
For Each ulElement In lists
For Each liElement In ulElement.getElementsByClassName("KambiBC-event-participants")
Set anchorElements = liElement.getElementsByClassName("KambiBC-event-participants__name")
If anchorElements.Length > 0 Then
Cells(row, 1) = anchorElements.Item(0).innerText
row = row + 1
End If
Next liElement
Next ulElement
End Sub
Screenshot

Internet Explorer VBA Automation Error: The object Invoked has disconnected from its clients

I'm trying to write code that will read a value from Excel, look it up in an internal web based system and store the results back in the Excel. It reads the Excel with no problem, opens Internet Explorer with no problem, but when I then try to reference what's been opened, I get the above error. The line "ie.Navigate url" works, but the next line "Set DOC = ie.Document" generates the error. Any ideas on what's causing this? Here's my code:
Public Sub getClient()
Dim xOpen As Boolean
xOpen = False
Dim row As Long
Dim xL As Excel.Application
Set xL = New Excel.Application
xL.Visible = False
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
'Change the name as needed, out put in some facility to input it or
'process multiples...
Dim filename As String
filename = "auditLookup.xlsx"
Set wb = xL.Workbooks.Open(getPath("Audit") + filename)
xOpen = True
Set sh = wb.Sheets(1)
Dim ie As Variant
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
Dim DOC As HTMLDocument
Dim idx As Integer
Dim data As String
Dim links As Variant
Dim lnk As Variant
Dim iRow As Long
iRow = 2 'Assume headers
Dim clientName As String
Dim clientID As String
Dim nameFound As Boolean
Dim idFound As Boolean
Dim url As String
While sh.Cells(iRow, 1) <> ""
'Just in case these IDs are ever prefixed with zeroes, I'm inserting
'some random character in front, but removing it of course when
'processing.
url = "https://.../" + mid(sh.Cells(iRow, 1), 2)
ie.navigate url
Set DOC = ie.Document
'Search td until we find "Name:" then the next td will be the name.
'Then search for "P1 ID (ACES):" and the next td with be that.
Set links = DOC.getElementsByTagName("td")
clientName = ""
clientID = ""
nameFound = False
idFound = False
For Each lnk In links
data = lnk.innerText
If nameFound Then
clientName = data
ElseIf idFound Then
clientID = data
End If
If nameFound And idFound Then
Exit For
End If
If data = "Name:" Then
nameFound = True
ElseIf data = "P1 ID (ACES):" Then
idFound = True
End If
Next
sh.Cells(iRow, 2) = clientName
sh.Cells(iRow, 2) = clientID
iRow = iRow + 1
Wend
Set ie = Nothing
If xOpen Then
wb.Save
Set wb = Nothing
xL.Quit
Set xL = Nothing
Set sh = Nothing
xOpen = False
End If
Exit Sub
Changing to:
Dim ie As InternetExplorer
Set ie = New InternetExplorerMedium
...
Solved the problem. Plus I did need to add back the Do loop mentioned in the comments:
Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE

Excel VBA navigation of html elements

I am working on a code that uses VBA-excel to navigate to a website. Once I have logged into the website, I am not able to interact with any of the html elements within the page. I need to be able to click on some check boxes and enter a date range into a field. Here is the code that I have. It works when I open a new window but I cant get it to work within the original ie window. I would like this process to be done in one window if possible. Any feedback is greatly appreciated.
Private Sub CommandButton1_Click()
Dim yesterday As String
yesterday = Date - 1
Dim RowCount As Integer
Dim x As Integer
Dim nik As Integer
'Opens Internet Explorer
Dim Shellwins As SHDocVw.ShellWindows
Dim Shellie As SHDocVw.InternetExplorer
Set Shellwins = New SHDocVw.ShellWindows
Dim ie1 As InternetExplorer
Strurl = "father.com"
Set ie1 = New InternetExplorer
ie1.Visible = True
ie1.navigate Strurl
Do While ie1.readyState <> 4 And ie1.Busy
DoEvents
Loop
Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
'MsgBox htmlel.Name
If htmlel.ID = "ctl00_Body_txtEmailAddress" Then
htmlel.Value = "dad#dad.com"
End If
Next
Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
'MsgBox htmlel.Name
If htmlel.ID = "ctl00_Body_txtPassword" Then
htmlel.Value = "mom"
End If
Next
Set htmldoc = ie1.document
For Each htmlel In htmldoc.getElementsByTagName("input")
If htmlel.ID = "ctl00_Body_btnLogon" Then
htmlel.Click
End If
Next
Strurl = "url.com"
Set ie2 = New InternetExplorer
ie2.Visible = True
ie2.navigate Strurl
ie1.Quit
Do While ie2.readyState <> 4 And ie2.Busy
DoEvents
Loop
Set htmldoc = ie2.document
For Each htmlel In htmldoc.getElementsByTagName("input")
If htmlel.ID = "ctl00_Body_txtStartDate" Then
htmlel.Value = yesterday
End If
Next
Set htmldoc = ie2.document
For Each htmlel In htmldoc.getElementsByTagName("input")
If htmlel.ID = "ctl00_Body_txtEndDate" Then
htmlel.Value = yesterday
End If
Next