I'm trying to connect to already opened IE window, but I have some problem at 2nd line
Run time error 429 ActiveX Component can't create object
I have added references also still not able to access the IE window. Below is the code.
Sub A()
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
if my_title Like "Put your webpage title here" & "*" Then
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
End Sub
Two things -
Declare 'ie' in your code.
IE_Count
IE_count = objShell.Windows.Count should be IE_count =
objShell.Windows().Count. Please refer -
https://msdn.microsoft.com/en-us/library/windows/desktop/bb773969(v=vs.85).aspx
Hope correcting these will get your code working
Related
I run this function with two IE browsers open. IE_count finds six objects, but it does not find any titles (my_title) within the for loop for objShell. They all return an empty string.
Any idea as to why this could be? Relevant code below:
' code below adapted from ron's answer here: https://stackoverflow.com/questions/21407340/how-to-read-text-from-an-already-open-webpage-using-vba
Function SecondBrowserSearchForAndClick(ElementID As String, searchFor As String)
Dim objShell
Set objShell = CreateObject("Shell.Application")
Dim IE_count As Integer
IE_count = objShell.Windows.Count
Dim x As Integer
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
Dim my_url As String
my_url = objShell.Windows(x).document.Location
Dim my_title As String
my_title = objShell.Windows(x).document.Title
If my_title Like "*Select Process*" Then 'compare to find if the desired web page is already open
Dim tagColl_TR As Object
Set tagColl_TR = objShell.Windows(x).document.getElementById(ElementID).contentDocument.getElementsByTagName("tr")
Dim f
While f < tagColl_TR.Length
If tagColl_TR(f).Children.Length = 5 Then
If tagColl_TR(f).Children(3).Children(0).innerText Like "*" & searchFor & "*" Then
tagColl_TR(f).Children(1).Children(0).Children(1).Focus
tagColl_TR(f).Children(1).Children(0).Children(1).Click
Exit Function
End If
End If
f = f + 1
Wend
End If
Next
End Function
Any help would be appreciated.
It's easier to put the "find document by title" functionality in its own function:
Sub Tester()
Dim doc As Object
Set doc = IEDocumentByTitle("Google")
If Not doc Is Nothing Then
Debug.Print "Found window at: " & doc.Location
'work on doc here
End If
End Sub
'Return an open IE document based on its Title property
Function IEDocumentByTitle(title As String)
Dim w As Object, ttl As String
For Each w In CreateObject("Shell.Application").Windows
If w.Application.Name = "Internet Explorer" Then 'filter out Windows Explorer
ttl = ""
On Error Resume Next
ttl = w.document.title
On Error GoTo 0
If ttl Like title Then
Set IEDocumentByTitle = w.document
Exit Function
End If
End If
Next w
End Function
This works fine for me.
BTW the shell Windows collection also includes Windows Explorer instances in addition to IE windows/tabs.
Also you should really cancel On Error Resume Next as soon as possible or it will silently swallow all errors in your code, possibly leading to unexpected results.
I inherited this VBA script from my predecessor. It works fine for me in Excel 2013 up until recently when I was told I may need to work from home. Come to find out, the Office 2016 environment of my newly accessed VPN desktop does not like this script. I keep getting "The remote server machine is unknown or unavailable" when it reaches .ReadyState <> READYSTATE_COMPLETE.
The navigation did not fail as I can see the window where it successfully navigated to the URL and I can interact with it correctly. The strange thing is if I change the URL to "www.google.com" I get a valid ready state result.
I also need to figure out how to late bind the Shell Windows so it will work with both the v15 and v16 libraries simultaneously.
The intent of this script is to automate a process that
1. Opens an internal database at DBurl via web interface
2. Manipulates and runs a java script located on the web page
3. Close the browser window without closing any other browser windows
This could be modified for someone else's use by looking for a page element, such as a search box or specific button on a page, and interacting with it.
Edit:
Additional testing has revealed that a pause at and skipping the Do While loop and resuming at IETab1 = SWs.Count results in this script working in Office 2016. The only issue, then, is without the loop, the page isn't yet ready for the next step when the script tries to run the interaction. A wait for 5 seconds in place of the loop band-aid's this issue. Finding why the .ReadyState won't read will fix this issue.
Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Sub OpenWebDB()
Dim ieApp As Object
Dim SWs As ShellWindows
Dim IETab1 As Integer
Dim JScript As String
Dim CurrentWindow As Object
Dim DBurl As String
Dim tNow As Date, tOut As Date
DBurl = "My.Database.url"
Set SWs = New ShellWindows
tNow = Now
tOut = tNow + TimeValue("00:00:15")
If ieApp Is Nothing Then
Set ieApp = CreateObject("InternetExplorer.Application")
With ieApp
.Navigate DBurl
Do While tNow < tOut And .ReadyState <> READYSTATE_COMPLETE
DoEvents
tNow = Now
Loop
IETab1 = SWs.Count
End With
End If
If Not tNow < tOut Then GoTo DBFail
On Error GoTo DBFail
Set CurrentWindow = SWs.Item(IETab1 - 1).Document.parentWindow
JScript = "javascript: DoSomething"
Call CurrentWindow.execScript(JScript)
On Error GoTo 0
SWs.Item(IETab1 - 1).Quit
Set ieApp = Nothing
Set SWs = Nothing
Exit Sub
DBFail:
MsgBox (DBurl & vbCrLf & "took too long to connect or failed to load correctly." & vbCrLf & _
"Please notify the Database manager if this issue continues."), vbCritical, "DB Error"
SWs.Item(IETab1 - 1).Quit
Set ieApp = Nothing
Set SWs = Nothing
End Sub
Try to remove the tNow < tOut from the Do While condition. Or, using the While statement to wait page complete:
While IE.ReadyState <> 4
DoEvents
Wend
The intent of this script is to automate a process that
1. Opens an internal database at DBurl via web interface
2. Manipulates and runs a java script located on the web page
3. Close the browser window without closing any other browser windows
Besides, according to the intent of the script, I suggest you could refer the following code (it could loop through the tabs, and close specific tab according the title):
Sub TestClose()
Dim IE As Object, Data As Object
Dim ticket As String
Dim my_url As String, my_title As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "https://www.microsoft.com/en-sg/" '1st tab
.Navigate "https://www.bing.com", CLng(2048) '2nd
.Navigate "https://www.google.com", CLng(2048) '3rd
While IE.ReadyState <> 4
DoEvents
Wend
'wait some time to let page load
Application.Wait (Now + TimeValue("0:00:05"))
'get the opened windows
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
'loop through the window and find the tab
For x = 0 To (IE_count - 1)
On Error Resume Next
'get the location and title
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
'debug to check the value
Debug.Print x
Debug.Print my_title
'find the special tab based on the title.
If my_title Like "Bing" & "*" Then
Set IE = objShell.Windows(x)
IE.Quit 'call the Quit method to close the tab.
Exit For 'exit the for loop
Else
End If
Next
End With
Set IE = Nothing
End Sub
I'm making a simple script which can open a site, find a button, click on it (open a new tab) and find data on the new tab. I'm blocked in a dump point : how can i access the data on the new tab ?
thanks a lot !
Sub testWebOpenTabAndFocus()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
' open new IE window
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
' Send the form data To URL As POST binary request
IE.navigate "somesite.com"
'..... some code to search about a button
objElement.Click ' click button to load a new tab
' Wait while IE re-loading...
While IE.Busy
DoEvents
Wend
' finding the new tab with Windows(x)
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_title = objShell.Windows(x).Document.Title
If my_title Like "*Analysis Application*" Then
Set IE = objShell.Windows(x) 'this is my new tab
End If
Next
'Some code to be runned on the new tab
'but its not working because it still has the focus on previous tab
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
Try to replace below code part in your above code may help to fix the issue.
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
' sometimes more web pages are counted than are open
If x.Name = "Internet Explorer" And TypeName(x.document) = "HTMLDocument" Then
my_title = objShell.Windows(x).document.Title
Debug.Print (my_title)
If my_title Like "*Analysis Application*" Then
Set IE = objShell.Windows(x) 'this is my new tab
End If
End If
Next
If issue persist than try to provide detailed information like on which line you are getting error or you stuck there with error message may give us more idea about the issue.
I have a VBA code that clicks a link on a IE page and a new window opens. How can I get the elements of that child window?
I tried with Shell.application. Are there any other methods?
Set objShell = CreateObject("shell.application")
IEcount= objShell.Windows.Count
For x = 0 To iecount
On Error Resume Next
Debug.Print x, objShell.Windows(x).document.Location
Next x
I got the window number and its URL but can't access the elements on the page.
Look at the "if-then" statement I've inserted within your code. You can use either the title or url to identify the child and set focus on it. Once it has focus, you can get the elements of the child window.
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
If my_title Like "Something that identifies your child window" Then
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
I have made a macro to read text from a saved html page/file. I now need to make it more advance by reading an already open webpage. would really appreciate the help
Need to replace the line
URL = "file:///C:/test.html"
with something that will read an open webpage. I can make sure that there is only one tab open. I'm using the latest IE
Dim URL As String
Dim Data As String
URL = "file:///C:/test.html"
Dim ie As Object
Dim ieDoc As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate URL
Do Until (ie.readyState = 4 And Not ie.Busy)
DoEvents
Loop
Set ieDoc = ie.Document
Data = ieDoc.body.innerText
If you know the title or url of the already open webpage you're looking for, then this code will let you control it
' Determine if a specific instance of IE is already open.
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
'You can use my_title of my_url, whichever you want
If my_title Like "Put your webpage title here" & "*" Then 'identify the existing web page
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
Use this code to get the currently running internet explorer (work atleast with IE9):
Dim ie As Object
Dim objShell As Object
Dim objWindow As Object
Dim objItem As Object
Set objShell = CreateObject("Shell.Application")
Set objWindow = objShell.Windows()
For Each objItem In objWindow
If LCase(objItem.FullName Like "*iexplore*") Then
Set ie = objItem
End If
Next objItem
MsgBox ie.Document.body.innertext
' Add reference to
' - Microsoft Internet Controls (SHDocVw)
' - Microsoft Shell Controls and Automation (Shell32)
' Find all running instances of IE and get web page Url
' Source: http://msdn.microsoft.com/en-us/library/windows/desktop/bb773974(v=vs.85).aspx
' Useful link: http://msdn.microsoft.com/en-us/library/windows/desktop/bb776890(v=vs.85).aspx
Sub main()
Dim browsers
Set browsers = GetBrowsers
Dim browser
Dim url
For Each browser In browsers
url = browser.document.Location.href
Debug.Print CStr(url)
Next browser
End Sub
Public Function GetBrowsers() As Collection
Dim browsers As New Collection
Dim shellApp As Shell32.Shell
Dim wnds As SHDocVw.ShellWindows
Set shellApp = New Shell
Set wnds = shellApp.Windows
Dim i As Integer
Dim ie As SHDocVw.WebBrowser
Dim name
For i = 1 To wnds.Count
Set ie = wnds(i)
If ie Is Nothing Then GoTo continue
If UCase(ie.FullName) Like "*IEXPLORE.EXE" Then
browsers.Add ie
End If
continue:
Next i
Set GetBrowsers = browsers
Set shellApp = Nothing
End Function