VBA Auotmation - Closing IE Before IE.Quit Runs Freezes Excel - vba

I have some code which closes IE after some VBA Website Automation (after waiting for a period of time).
Application.Wait (Now + TimeValue("00:00:55"))
IE.Quit
Set IE = Nothing
Set doc = Nothing
The problem is that if someone inadvertently closes IE before the IE.Quit code executes, Excel freezes because the IE property no longer exists.
I've tried something like this (please see below code excerpt) but Excel still freezes if it's closed before the timer executes the code (Run-Time error '462', The remote server machine does not exist or is unavailable):
Application.Wait (Now + TimeValue("00:00:55"))
If IE Is Nothing Then
'do nothing
Else
IE.Quit
Set IE = Nothing
Set doc = Nothing
End If
Any ideas or guidance would be much appreciated.

I'm agree with what Mathieu says. The freeze is due to the waiting is still being executed. Everything you put after Application.Wait will be executed after the waiting--which is to say after "the freeze".
You can only add On Error Resume Next after Application.Wait (Now + TimeValue("00:00:55")) to avoid the error display, but you can't stop the waiting.

Related

VBA random automation error in CreateObject("InternetExplorer.Application")

So...
I searched through the forum, and really found a lot of threads with awesome suggestions, but none of them works (Example
Thing is my code randomly breaks on Set ie = CreateObject("InternetExplorer.Application"). After resuming code it is always continuing without a problem. Error msg is:
Run-time error '-2147467259 (80004005)' Automation error Unspecified error
After each iteration I do ie.quit and set ie = nothing and additionally I checked and clear processes, just in case if internetexplorer would pile up and suffocate my computer in the background.
Something like:
If CheckIFRunning("INTERNET EXPLORER.EXE") = True Or CheckIFRunning("IEXPLORE.EXE") = True Then oProc.Terminate
And like many suggested I tried Set ie = New InternetExplorerMedium but with no luck.
I added application.wait as I thought maybe there is a split second where IE is still not close and I'm already starting a new instance but that didn't help as well.
I can add a code, but it is extremely long and my only problem is on CreateObject.
I would like to at least resume execution even if for some reason really there is a problem with creating object (and yes, I've tried on error resume next).
Try this, hope it will help you .
replace
Set ie = CreateObject("InternetExplorer.Application")
with
' Ignore the error
On Error Resume Next
' if IE already open then try to get the object
Set ie = GetObject(, "InternetExplorer.Application")
' Check is there error after the get object
If Err.Number <> 0 Then
' if an error happen then try to create new IE
Err.Clear
Set ie = CreateObject("InternetExplorer.Application")
End If
' Turn Error reporting back on
On Error GoTo 0

how to close pop-up explorer massage box?

I'm using VBA to learn programming.
In the process of scraping web pages with createobjct function, I am seriously stuck with problems with the following message box.
I can't go forward with VBA without closing below box. Contents of message is simple, there is no data in that page. (It is EC site)
...
ie.Navigate strURL
ie.Visible = False
'Waiting with do while
Do While (ie.ReadyState <> READYSTATE_COMPLETE Or ie.Busy = True)
DoEvents
Loop
'2seconds wait more
Application.Wait (Now + TimeValue("00:00:02")) ' Here is the stop point with message box.
Is there a way to close the popup message box?

VBA Excel - Open IE window to URL, pause, then close

I am limited to using VBA to accomplish this. The goal is to programmatically Open a new IE window which is a duplicate of a window already open.
I need to display this window for limited amount of time (in this example I am waiting 15 seconds), then I want to close one of the two IE windows I have open.
I have cobbled together code fragments from a few examples I have found and this is partially working, but the results are not as I would expect.
First I am able to find the IE instances but even though I think I have coded an exit, both windows are closed.
The MsgBox I am using for debugging never appears.
With each run of the code the error message below appears
Below is the code I am trying to get to work, but failing with.
Private Sub OpenReport()
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://~~~~~~~~~.net/reports/views/result/reportResult.faces"
' Wait for a period of time contained in TimeValue
Application.Wait (Now + TimeValue("00:00:15"))
' Now close ONE of the IE windows (Currently closing all of them)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
' Find IE Instances
For Each objItem In colItems
If objItem.Name = "iexplore.exe" Then
On Error Resume Next
objItem.Terminate ' Terminates all instead of exiting after finding one IE window
MsgBox objItem.Name & " " & objItem.ProcessID & " " & objItem.CommandLine 'Doesn't appear
Exit For
End If
Next
End Sub
I appreciate the input but had to go a slightly different route to get this working as it should...
The key to getting one instance of IE to close was solved by using TaskKill (commandline WScript).
Below is the full solution
Private Sub OpenReport()
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://~~~~~~~~~~~net/reports/views/result/reportResult.faces"
' Wait for a period of time contained in TimeValue
Application.Wait (Now + TimeValue("00:00:15"))
' Now close ONE of the IE windows (Currently closing all of them)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
' Find an IE instance
For Each objitem In colItems
If objitem.Name = "iexplore.exe" Then
On Error Resume Next
Shell ("TaskKill /PID " & objitem.ProcessID)
Exit For
End If
Next
End Sub

Automation Error "The object invoked has disconnected from its clients"

I'm creating vba sub in excel to automate getting data from the website. But I'm stuck with clicking an element. I kept receiving the error Run-time error -02147417848 (80010108). I don't know if I had missed something. I already tried using its id and name but it's just the same.
Sub audit()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
With ie
.Visible = True
.navigate "https://samplesite.com"
End With
While ie.Busy
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:05"))
ie.document.getElementsByClassName("xxxxxxx")(0).Click // usually the error is here
End Sub

VBA with Excel and IE: Can't get the source right source code?

I'm currently trying to automate my IE at work with VBA and Excel, because we have some repetitive tasks to complete in an SAP System which is called within the Internet Explorer.
So far I am able to start IE and navigate to the starting page of our System.
I looked on the source code from the Sap System to search for an id from a div (lets say its 'SampleID'). But if I try to click it via VBA I only get returned 'nothing'. So I tried to print out the source code via VBA Debug.Print to look at the problem. And here's the thing:
The source code that gets printed is completely different from the Website's Source Code. In the printed code for example is a iframe that I can't find in the website source code. So that's the reason I can't find my div and any other objects, because they are not in the code that VBA is searching through.
Can anyone tell me what's going wrong? Why are the source codes completely different?
Sub stgGo(){
Dim i As Long
Dim IE As InternetExplorerMedium
Dim objElement As Object
Dim objCollection As Object
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate ("URL")
'waiting 10 seconds to be really sure javascript is ready
Application.Wait (Now + TimeValue("0:00:10"))
'printing out the false source code
Debug.Print strHTML = myIE.Document.body.innerHTML
Set objCollection = IE.document.getElementsByTagName("div")
i = 0
While i < objCollection.Length
If objCollection(i).ID = "SampleID" Then
objCollection(i).Click
End If
i = i + 1
Wend
End Sub