VBA Compile error What is the cause and solution? - excel

Set myIE = New InternetExplorer
myIE.Silent = True
myIE.navigate sURL
myIE.Visible = False
Do While myIE.Busy
Do Until myIE.ReadyState = READYSTATE_COMPLETE
Loop
Loop
Application.Wait (Now() + TimeValue("00:00:02"))
Set HTMLDoc = myIE.document
HTMLDoc.getElementById("loginID").Value = ICUSER
HTMLDoc.getElementById("password").Value = ICPASS
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
Application.Wait (Now() + TimeValue("00:00:02"))
' Read and Write Data to the reports
Application.DisplayAlerts = False
End Sub
I have been using this code for a while and It works just fine on my computer, but when I share the file with others some computers can not run the code and gives this error. Any one know the solution to this issue, please help.

My best guess is that there is a different in versions of Internet Explorer. When faced with version issues between different computers in VBA you can switch from using Tools>>References to Late binding.
To use late binding here change your code from
Set myIE = New InternetExplorer
To:
Set myIE = CreateObject("InternetExplorer.Application")
Now, as long as the other computer has any version of Internet Explorer installed, this code will work.

Check that the references mentioned in the code are sill set (Tools>References...). If that doesn't work, last resort is to change the order of the references (Tools menu in vb window). Unfortunately it is just trial and error..

Related

Scraping source code of website does not work on VDI

I have a problem with extracting data from the website using VBA on Citrix Virtual Desktop.
I have wrote my code on my local desktop first and it works good - HTML source has been extracted to the cell in Excel.
On VDI IE opens the website without any problems.
Code:
Sub GetBody()
Dim Body As String
the_start:
Set ObjIE = CreateObject("InternetExplorer.Application")
ObjIE.Visible = False
ObjIE.navigate ("https://pl.wikipedia.org/wiki/Wikipedia:Strona_g%C5%82%C3%B3wna")
Do
DoEvents
If Err.Number <> 0 Then
ObjIE.Quit
Set ObjIE = Nothing
GoTo the_start:
End If
Loop Until ObjIE.readyState = 4
Body = ObjIE.document.Body.innerHTML
Cells(1, 1).Value = Body
End Sub
When I try to run this code on VDI I am getting following error:
Run-time error '-2147467259(80004005)': Method 'Document' of object 'IWebBrowser2' failed.
Any ideas where this error comes from and what I should add to run it successfully on VDI?
I have done some changes mentioned in the comments (like changing the endless loop etc.) and also have another errors ( Automation error The object invoked has disconnected from its clients).. Previously I have declared IE as a object in this line below:
Set ObjIE = CreateObject("InternetExplorer.Application")
Soultion for all my problems:
Dim IE as SHDocVw.InternetExplorer
Set IE = New InternetExplorerMedium
Thank you all for participating in this thread and THANK YOU SO MUCH for your help!

Windows 10 Internet Explorer is not working with old VBA code

I wrote code in VBA a couple years ago to open a website in IE and fill in textboxes with data from an excel file. Unfortunately I am using windows 10 now and this program is not working anymore. It opens the website with no problem, but cannot transpose the data into the textboxes. It simply opens the website and stalls (no data is entered).
The program still works in IE in windows 7, without any problem. I tried multiple laptops with windows 10 and the problem reoccurs.
I honestly don't know how to fix this.
DoEvents
WebAddress = "CONFIDENTIAL URL HERE" & WebID & "&t="
Dim IE As Object
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate WebAddress
IE.Visible = True
While IE.busy
DoEvents 'wait until IE is done loading page.
Wend
Application.Wait (Now + TimeSerial(0, 0, 4))
IE.Document.All("Response_123").Value = ThisWorkbook.Sheets("Sheet1").Range("B5")
The last line of code should transfer data from the excel file to the textbox in internet explorer, however nothing happens and the textbox remains blank. Do I need to change anything in my code to account for windows 10 IE?
You can see that your code was not written in an efficient way. For an alternative approach, You can refer example below which is working fine with Windows 10.
Dim IE As Object
Sub demo()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "C:\Users\Administrator\Desktop\sample.html"
While IE.Busy
DoEvents
Wend
wait 1
IE.Document.getElementById("firstname").Value = ThisWorkbook.Sheets("Sheet1").Range("A1")
wait 1
IE.Document.getElementById("lastname").Value = ThisWorkbook.Sheets("Sheet1").Range("A2")
wait 1
IE.Document.getElementById("submit_btn").Click
IE.Quit
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
Private Sub wait(seconds As Long)
Dim endTime As Date
endTime = DateAdd("s", seconds, Now())
Do While Now() < endTime
DoEvents
Loop
End Sub
Output:
If we talk about your above posted code than you can try to put the break point on that line and try to debug the code. Check that Which value contained by Cell B5 and check whether that line get execute successfully or not. For testing purpose, try to assign static value to that textbox.

VBA Internet explorer Automation error

I was trying to set up a public test environment to see if anyone would be able to help me with another question I asked this morning and I'm getting this error which I was not getting in my original code and after browsing a bit around I cannot fix: Automation error - The object invoked has disconnected from its clients.
Here is the full code:
Sub GetBranches()
Dim objIE As InternetExplorer
Set objIE = New InternetExplorerMedium ' create new browser
objIE.Visible = True
objIE.navigate "https://casadasereia.net/vbatests/viewtree241653.html"
' wait for browser
Do While objIE.Busy = True Or objIE.readyState <> 4
DoEvents
Loop
End Sub
Anyone knows how to fix this?
Use late-binding to avoid additional reference to a library and this will fix versioning issues if any.
Sub GetBranches()
Dim objIE as Object
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True
objIE.navigate "https://casadasereia.net/vbatests/viewtree241653.html"
' wait for browser
Do While objIE.Busy = True Or objIE.readyState <> 4
DoEvents
Loop
End Sub

VBA extract text value from webpage?

I have a webpage with some text in a HTML Span like so:
<span id="ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate">Expiry Date : 16/02/2018</span>
I am trying to get this value display it as a message in excel using the below code:
Sub PullExpiry()
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")
With appIE
.Navigate "https://www.brcdirectory.com/InternalSite//Site.aspx?BrcSiteCode=" & Range("J6").Value
.Visible = True
End With
Do While appIE.Busy Or appIE.ReadyState <> 4
DoEvents
Loop
Set getPrice = appIE.Document.getElementById("ctl00_ContentPlaceHolder1_FormView1_GridView1_ctl02_lb_ExpiryDate")
Dim myValue As String
myValue = getPrice.innerText
appIE.Quit
Set appIE = Nothing
MsgBox myValue
End Sub
This was working on my laptop (operating windows) but it does not work on my computer (also operating windows). Both windows are the same version with the same version of IE. I cannot explain it.
I have Microsoft Office and Excel Object libraries turned on in both references.
I get an error about an active x component not being able to create something
Please can someone show me where i am going wrong?
You need to add Microsoft Internet Controls to your references to be able to create the object. You may have to register the shdocvw.dll library on your computer. It may be registered on your laptop already which is why it might be bombing on your computer.
How to register a .dll
MSDN Documentation: look at the last sentence before the C# example.
Similar Question
After playing around a little bit and ensuring everything was registered, this ran fine on my PC:
Public Sub ie()
Dim ieApp As SHDocVw.InternetExplorerMedium
Dim html As HTMLDocument
Set ieApp = New SHDocVw.InternetExplorerMedium
ieApp.Visible = True
ieApp.Navigate "http://internalAddress/"
Do While ieApp.Busy Or ieApp.ReadyState <> 4
DoEvents
Loop
Set html = ieApp.Document.getElementById("myID")
End Sub

VBA Excel interacting with IE, gives unusual runtime error

I'm using some code found here elsewhere on Stack, but I get an unusual run time error that I can't seem to find any info on:
Run-time error '-2147023179 (800706b5)'
Automation error
The interface is unknown.
With so little info on it, and what info existing being barely relevant, I started to think it has to do with References. I added one that I read about that seemed like it could help (Microsoft Internet Controls), but it didn't. Am I missing something others that may be required? Any direction would really be helpful.
The error occurs on the following line of code:
IE.Document.getElementById("CRM_Number").Value = "CRM12345"
This is the ORIGINAL code, which I only changed the website and the ElementID (can only access on VPN so won't help to link here). Presumably, this will work fine unless it's been changed since posted:
Dim IE As Object
Sub submitFeedback3()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://spreadsheetbootcamp.com/excel-efficiency-trainer-feedback/"
Application.StatusBar = "Submitting"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
**********************************************************************
IE.Document.getElementById("Form_Attempts-1372643500")(0).Value = "1"
IE.Document.getElementById("submit-1-1372643500")(0).Click
**********************************************************************
Application.StatusBar = "Form Submitted"
IE.Quit
Set IE = Nothing
Application.ScreenUpdating = True
End Sub

Resources