vba internet explorer delete object - vba

I'm making simple vba script witch is gonna open IE, get some informations and exit. Script open IE 35 times without problem, but after that it, works, but doesn't open IE anymore.
Sub Test()
for x=1 to 50
Dim objIE As Object
Set objIE = New InternetExplorer
Set objIE = New InternetExplorerMedium
objIE.Visible = True
objIE.Navigate2 "http://www.google.com"
objIE.Visible = False
Set objIE = Nothing
next x
End Sub

I don't really get what you want do with your code, but here some tips that might help to fix the issue:
You can take both the declaration and the set of your browser out of the loop, you don't need to declare it and set it each time:
Dim objIE As Object
Set objIE = New InternetExplorerMedium
Why are you setting objIE as New InternetExplorer, if you're right after setting it again as New InternetExplorerMedium? That action is useless.
Between the objIE.Navigate2 "http://www.google.com" and the Set objIE = Nothing you should probably wait some time, at least with a Do While objIE.busy Loop, because you don't even give the browser the time to load the document and you already destroy it.
The Set objIE = Nothing can also be put out of the loop, you can re-use the same browser to navigate as many links as you want. Also, don't forget to quit it first before destroying it from memory with objIE.quit
If it works 35 times and keep on "working but doing nothing" starting from the 36th, wonder about the links are corrupted when you get there.
You can check that with a Debug.Print on the link you're going to navigate each time.

This is the basic structure to automate IE with VBA.
Option Explicit
Sub PAO()
Dim IE As InternetExplorer
Dim url As String
Const url$ = "http://www.google.com"
Set IE = CreateObject("internetexplorer.application")
IE.Visible = True
IE.navigate url
Set IE = Nothing
End Sub
Also, here is a Wait function that can help you to wait for the webpage to load.
Function IEWait(t As Long)
Do While IE.Busy
Application.Wait DateAdd("s", t, Now)
Loop
End Function
This is how you use the function:
IEWait (1)
Do Until IE.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Hope this will help you but let me know if you have any question.

Related

The remote server machine does not exist or is unavailable for specific website

I tried to open website using below VBA code:
Dim IE As Object
Dim doc As Object
Dim strURL As String
Set IE = CreateObject("InternetExplorer.Application")
With IE '
.Visible = True
.navigate "https://Google.com"
Do Until .readyState = 4
DoEvents
Loop
It's working for website like "google" etc.
But when I tried to open specific site like my company PLM " Agile (https://agileplm.XXXX.com/Agile/default/login-cms.jsp)" throwing error
"The remote server machine does not exist or is unavailable"
I could open the web page on explorer but throwing error while executing from below line
Do Until .readyState = 4
DoEvents
Loop
Is this due to any protection over site or not?
I used early binding on this and created the object InternetExplorerMedium rather than InternetExplorer and it seemed to work.
The issue is that Internet Explorer disconnects from VBA (In my case some internal security settings). The solution is to reconnect Internet Explorer to VBA:
Include this code after the line IE is not responsive anymore and the existing Internet Explorer window will be assigned to = IEObject1
Dim shellWins As SHDocVw.ShellWindows
Dim explorer As SHDocVw.InternetExplorer
Set shellWins = New SHDocVw.ShellWindows
For Each explorer In shellWins
If explorer.Name = "Internet Explorer" Then
Set IEObject1 = explorer
Debug.Print explorer.LocationURL
Debug.Print explorer.LocationName
End If
Next
Set shellWins = Nothing
Set explorer = Nothing
If you have several IE windows open then this code picks the last one. You can choose between the windows by using the URL or LocationName if you need several open windows.
Try to make a test with example code below may help you to solve your issue.
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://agileplm.xxxx.com/Agile/default/login-cms.jsp"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
Reference:
Automate Internet Explorer (IE) Using VBA
If your issue persist than try to provide a detailed information, Whether you are having issue with opening the page or you got an error while checking the ready state of IE. We will try to provide further suggestions.
Dim IE As Object
Dim doc As Object
Dim strURL As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate "https://Google.com"
End With
For Each win In CreateObject("Shell.Application").Windows
If win.Name Like "*Internet Explorer" Then
Set IE = win: Exit For
End If
Next
With IE
Do Until .readyState = 4
DoEvents
Loop

Error 91 Search and scrape web

I am new to VBA, and trying to put together a code that will allow me to search a website for bond information, which is listed in my excel file, and pull back the bond's issue date. I have been able to get to the website, and using F8 to execute the code manually, the code appears to work fine. However, when I run the macro, I get
Error 91: Object variable or With block variable not set.
I am unsure how to fix this issue, and I have tried to find earlier answers, as there is a lot of info on error 91. But none seem to be able to help with my specific problem.
Please assist, thanks.
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim datelabel As String
Dim x As String 'for the CUSIP number
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = False
'navigate IE to this web page
objIE.navigate "https://emma.msrb.org/Search/Search.aspx"
'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
'DATA SCRAPING Portion
Dim Doc As HTMLDocument
Set Doc = objIE.document
'get issue date
datelabel = Doc.getElementsByClassName("value")(0).innerText 'HERE IS WHERE I HAVE MY PROBLEM
'MsgBox datelabel
End If
Loop
'close the browser
objIE.Quit
This is the html that I am trying to pull:
span class="value"
from:
https://emma.msrb.org/SecurityView/SecurityDetails.aspx?cusip=ACDB05F7DCC14B929AC9D2D082A3D9AE0

Clicking link within a HTML List

I'm trying use Excel's IE automation tools to click a website link within a users profile of this site. Using the following in VBA:
ie.Document.getElementsByClassName("website")(0).getElementsByTagName("a")(0).Click
But keep getting the Runtime error '438' when ran. Any advice on how to fix this or if clicking this link is even possible please? Thanks.
EDIT (Full Code):
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "site"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set objA = ie.Document.getElementsByClassName("website")(0) 'GETTIN ERROR HERE
objA.getElementsByTagName("a")(0).Click
End Sub
It would appear that there were a few minor issues here.
One being that .Click doesn't always function properly, I would suggest using .getAttribute("href") combined with ie.Navigate()
Another being that you seem to define html then never use it again.
The following code works:
Sub test()
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://beverlyhills.yourkwoffice.com/mcj/user/AssociateSearchSubmitAction.do?orgId=5058&lastName=&firstName=&rows=100"
Do While ie.READYSTATE <> READYSTATE_COMPLETE
DoEvents
Loop
Set html = ie.Document
Set objA = html.getElementsByClassName("website")(0).getElementsByTagName("a")(0)
ie.Navigate (objA.getAttribute("href"))
End Sub

vba error message 13 IE.document

I see that someone else has asked the same question but the answer didn`t help so I am having to raise it once more,
I am running Windows 10 internet explorer 11, 64 bit
I get error mismatch error 13 at last line
I have tried Dim IE As New InternetExplorer and Dim IE As New InternetExplorerMedium but still same error ..
any idea who I can get rid of the error please ?
Thanks
Sub test()
Dim IE As Object ' InternetExplorer
Set IE = CreateObject("internetexplorer.application")
Dim html As HTMLDocument
Set IE = New InternetExplorer
IE.Visible = True
IE.navigate "http://stackoverflow.com/"
'wait while web page load
Set html = IE.document
end sub
Try using late binding instead for the HTMLDocument. Typically you don't need to assign the document to an object, you can just interact with it without assigning it to something else.
Sub test()
Dim IE As New InternetExplorer ' Make sure you have Microsoft Internet Controls Reference added
Dim html As Object ' Or you can use HtmlDocument as the type if Microsoft HTML Object Reference is added
With IE
.Visible = True
.navigate "http://stackoverflow.com/"
While .busy Or .readystate <> 4
Application.Wait (Now() + TimeValue("00:00:01"))
DoEvents
Wend
Set html = .document
End With
End Sub

Automatically uploading a picture file to a free online OCR resource

I am trying to upload a .jpg file to a free online OCR site. I am using Excel VBA for this project:
Sub getOcrText()
Dim ocrAddress As String: ocrAddress = "http://www.free-online-ocr.com"
Dim picFile As String: picFile = "C:\Users\310217955\Documents\pdfdown\test.jpg"
Dim elementCollection As Variant
Dim IE As New InternetExplorerMedium
With IE
.Visible = True
.Navigate (ocrAddress)
Do While IE.Busy: DoEvents: Loop
Set elementCollection = IE.document.getElementsByName("fileUpload")
End With
IE.Quit
Set IE = Nothing
End Sub
However, when I run the code to see whether I get objects to elementCollection I get a Runtime error, automation error, unspecified error, the code successfully navigates to the desired webpage.
How do I overcome this error?
You need to change a couple lines.
First this one:
Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
.
The second problem...
IE.Busy is not a sufficient test. Make that line the following instead:
Do While (IE.Busy Or IE.READYSTATE <> 4): DoEvents: Loop