Internet Explorer child window in VBA - vba

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

Related

How to select IE tabs after simulate click

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.

Control Internet Explorer from VBA

I have created a macro/userform that creates an html file and want Internet Explorer 11 to show the page.
When you use the userform you create new data that should be shown on the page. How do I refresh the page on IE11?
I tried using meta refresh, but if the userform writes the page at the moment IE refreshes the page goes blank and stops from refreshing.
Another method I tried is sendkeys but it doesn't seem to work.
AppActivate "The page - Internet Explorer"
SendKeys "{F5}", True
nothing happens.
Are there any other options to make sure the page is refreshed and always visible? (meaning don't refresh when the file is in use by the userform)
If page is already opened then you need to find that specific page and then you can try to refresh it. you can try to find the page by its title.
See the example code below.
Sub demo1()
flag = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
If my_title Like "Microsoft" & "*" Then
Set ie = objShell.Windows(x)
ie.Refresh
flag = 1
Exit For
Else
End If
Next
If flag = 0 Then
Debug.Print ("A matching webpage was NOT found")
Else
Debug.Print ("A matching webpage was found")
End If
End Sub
Try to modify the title in code to match with your web page.

VBA IE automation: loop for opening multiple tabs with identical names and URLs and performing same action in them

Suppose I have a list of identical URLs in spreadsheet column.
I navigate to the URL manually, then I want my macro to open every URL in separate IE tab and also click a specific button in it.
I use custom function FindIEObject to grab browser window by name or URL.The problem is that obviously each tab will have the same name and URL, so I need to find another way to differentiate between them and set each one as current IE object. My first thought was to assign a dummy name or a number to each tab as it gets created. I suppose this part should go somewhere after IE.Navigate Cell.Value in the loop.
Here is my code, currently opening all the tabs, but clicking the element only in the first one:
Private page_title As String
Public IE As InternetExplorerMedium
Sub intercept()
FindIEObject ("Allegro")
For Each Cell In Columns("A").Cells.SpecialCells(xlConstants)
IE.Navigate Cell.Value, CLng(2049) ' open URL in new tab
'Call MyWait
For Each element In IE.Document.GetElementsByTagName("img")
If element.GetAttribute("src") = "https://assets.allegrostatic.com/opbox/allegro.pl/homepage/Main%20page%20-%20new_SG_categories%20-%20icons/c4c8134ad8ae75c9d1070e7bee8cbeea.svg" Then
element.Click
Exit For
End If
Next element
Next Cell
End Sub
Sub MyWait()
Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
End Sub
Public Function FindIEObject(target As String) As InternetExplorerMedium
marker = 0
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
page_url = objShell.Windows(x).Document.Location
page_title = objShell.Windows(x).Document.Title
If page_title Like target & "*" Then 'compare to find if the desired web page is already open
Set IE = objShell.Windows(x)
marker = 1
Exit For
End If
Next
If marker = 0 Then
MsgBox ("A matching webpage was NOT found")
Else
MsgBox ("A matching webpage was found")
End If
End Function
I would be grateful for any suggestions.

VBA for Connecting already opned IE window

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

VBA - Addressing Internet Explorer tabs

Strangely enough I didn't find any information on the topic and I'm currently stuck at the point where I managed to open a new tab in an instance of IE by programmatically clicking a button, but I haven't the faintest clue of how to address the new tab in order to get information from there (the button basically brings up a new tab with the result of a search).
This is basically a straightforward question, but I'm including my code anyway:
Sub AddInfoFromIntranet()
Dim Ie As SHDocVw.InternetExplorer
Dim URL As String
Dim iFrames As MSHTML.IHTMLElementCollection
Dim iFrame As MSHTML.HTMLFrameElement
Dim Doc As MSHTML.HTMLDocument
Dim InputBox As MSHTML.IHTMLElementCollection, htmlButton, allTags, Tag
' Opens Intranet - yeah, sadly it's not a public web page
URL = "{My intranet website}"
Set Ie = New SHDocVw.InternetExplorer
With Ie
.navigate URL
.Visible = True
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set Doc = .document
End With
' Gets top_window frame and navigates to it, then inserts the name to search
Set iFrames = Doc.getElementsByName("top_window")
If Not iFrames Is Nothing Then
Set iFrame = iFrames(0)
Ie.navigate URL & iFrame.src
While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
Set InputBox = Doc.getElementsByName("Nachnamevalue")
If Not InputBox Is Nothing Then InputBox(0).Value = "test"
' Clicks on "search"
Set allTags = Doc.getElementsByTagName("input")
For Each Tag In allTags
If Tag.Value = "suchen" Then
Tag.Click
Exit For
End If
Next
' Here a new tab is opened, must find info in this tab
While Ie.Busy Or Ie.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
' HERE I HAVE NO CLUE WHAT TO WRITE. THE CODE ABOVE WORKS FLAWLESSLY
End If
Set Doc = Nothing
Set iFrames = Nothing
Set iFrame = Nothing
Set InputBox = Nothing
Set allTags = Nothing
Set Ie = Nothing
Ie.Quit
End Sub
Now, is there a way to address a tab by: 1) its name (and where do I find it) 2) its position in browser 3) the status (if it is "active") ?
Bonus questions: since I am new to VBA and Internet Explorer interaction, what exactly are the variables: htmlButton, allTags, Tag ? Also, could anyone explain if I need to set all the variables at the end to nothing, or I just need to set the Internet Explorer to nothing?
Thanks in advance!
See below for a function you can use to get an open IE document window - I don't think IE exposes any simple (VBA-accessible) API for working directly with tabs or determining whether a specific tab is active.
allTags is a collection of DOM elements with type "" , and Tag is a single memeber of that collection.
You do not have to set objects to Nothing before exiting a Sub (though some people still do that) - the VBA runtime will take care of that for you.
Sub TestGetIE()
Dim IE As Object
Set IE = GetIE("http://stackoverflow.com")
If Not IE Is Nothing Then
IE.document.execCommand "Print", False, 0
End If
End Sub
'Get a reference to an open IE window based on its URL
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
sURL = ""
On Error Resume Next
'check the URL and if it's the one you want then
' assign it to the return value
sURL = o.document.Location
On Error GoTo 0
'Debug.Print sURL
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function