VBA Macro For Already Open IE Window

RL101

New Member
Joined
May 29, 2011
Messages
1
Hi all,

I'm looking to create a VBA macro which will be ran from Excel (2003 or 2010) & which works with Internet Explorer. I have added 'Microsoft Internet Controls' to VBA References, and have looked at plenty of VBA code on the forum which opens & then navigates to specific websites.

However, what I need is for this macro to affect an already open IE window.

Does anyone have an idea of the syntax / command to have excel "select" a specific IE window already open?

Many thanks!
 
ZVI,

Great stuff!!! I need to accomplish the identical task, but must do using Chrome. Any assistance would be greatly appreciated.

Thanks





Hi & Welcome to the Board!
Try this:
Rich (BB code):

' ZVI:2011-05-30 VBA Macro For Already Open IE Window
' Reference required: Tools - References - Microsoft Internet Controls
Sub GetIE()
  
  Dim shellWins As ShellWindows
  Dim IE As InternetExplorer

  Set shellWins = New ShellWindows

  If shellWins.Count > 0 Then
    ' Get IE
    Set IE = shellWins.Item(0)
  Else
    ' Create IE
    Set IE = New InternetExplorer
    IE.Visible = True
  End If

  IE.Navigate "http://support.microsoft.com/kb/q176792/"

  Set shellWins = Nothing
  Set IE = Nothing

End Sub
Regards,
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Upvote 0
Late binding version, reference to Microsoft Internet Controls is not required:
Rich (BB code):

Sub GetIE_LateBinding()
  
  Dim IE As Object
  
  With CreateObject("Shell.Application").Windows
    
    If .Count > 0 Then
      ' Get IE
      Set IE = .Item(0) ' or .Item(.Count - 1)
    Else
      ' Create IE
      Set IE = CreateObject("InternetExplorer.Application")
      IE.Visible = True
    End If
  
    IE.Navigate "http://support.microsoft.com/kb/q176792/"
  
    Set IE = Nothing
  
  End With
  
End Sub
P.S. Window of any MS Explorer can be catched, not only of MS Internet Explorer.

Hi Vladimir,

Thank you for the above code which helps me to open web page in same tab instead of to open second window or tab.

I need your help to go next step. using your above code i would like to add the following steps :

1 - Copy Cell A2 (sheet name : Sheet1)

2 - Go to Opened tab "www......... [ using Valadimir code GetIE_LateBinding() which is working perfectly]

3 - Paste the above copied cell value (eg A2) in Job Number Box [ HTML CODING
input name="0_31"tabindex="2" title="Job Number" class="textfieldautosuggesttextfield" id="C0_31"

4 - Go & Click the search button to search above job number [ HTML Coding
img name="hc_Find" title="Find(Ctrl+Alt+I)" id="hc_Find" ***********="this.src='/jde/img/hc_Findmo.gif'"**********="this.src='/jde/img/hc_Find.gif'"*******="javascript:JDEDTAFactory.getInstance('').post('0_15')"alt="Find (Ctrl+Alt+I)" src="/jde/img/hc_Find.gif"border="0"

5 - Click button >| to go Last line [HTML Coding

a title="Go to end"id="GOTOLAST0_1" style="text-decoration: none;"href="javascript:*******=JDEDTAFactory.getInstance('').post('gLS0_1')"><imgname="jdehtmlGridDown" title="Go to end" id="jdehtmlGridDown"***********="this.src='/jde/img/alta/grid/last_ovr.png'"**********="this.src='/jde/img/alta/grid/last_ena.png'"*******="" alt="Go to end"src="/jde/img/alta/grid/last_ena.png" border="0"></a>

img name="jdehtmlGridDown"title="Go to end" id="jdehtmlGridDown"***********="this.src='/jde/img/alta/grid/last_ovr.png'"**********="this.src='/jde/img/alta/grid/last_ena.png'"*******="" alt="Go to end"src="/jde/img/alta/grid/last_ena.png" border="0">

6 - Click export button to Export data. HTML Coding

Script jdeWebGUIstartMenu(px"ToolsMenu");
jdeWebGUIaddMenuItem("ToolsMenu","Export Grid Data","javascript:toolsExitSelected('ExportData0010_1', '')","MENU","Export_To_Excel","Menu Item","",false,"","","false","");

7 - Click Continue button to save the above export data :

a tabindex="-1" title="Continue(Ctrl+Alt+E)" style="text-decoration: none;"><imgname="hc1" title="Continue (Ctrl+Alt+E)" id="hc1"***********="this.src='/jde/img/hc1mo.gif'"**********="this.src='/jde/img/hc1.gif'" *******="checkExportParams('');"alt="Continue (Ctrl+Alt+E)" src="/jde/img/hc1.gif"border="0"></a>

img name="hc1" title="Continue(Ctrl+Alt+E)" id="hc1"***********="this.src='/jde/img/hc1mo.gif'"**********="this.src='/jde/img/hc1.gif'"*******="checkExportParams('');" alt="Continue (Ctrl+Alt+E)"src="/jde/img/hc1mo.gif" border="0">

8 - after save the above export data go to next cell (A3)

9 - stop looping if Column A get blank (means end of job numbers) & show the msg Total Job numbers has been exported.

Any idea for the above ?

Your cooperation would be really really appreciated.

Best Regards,

Misbah
 
Upvote 0
Hi Matt,
Welcome to the Board!
Seems it's a bit different task, hope you mean something like this:
Rich (BB code):
' Code of Module1

Sub LoopIE(Optional Cancel As Boolean)
  
  ' --> Settings, change to suit
  Const Link = "www.google.com"
  Const RepeatMinutes = 5   ' Time interval in minutes for repeating of this macro
  Const WaitSeconds = 10    ' Waiting time interval in secondss for "IE is ready" state
  ' <-- End of settings

  Dim t As Single
  Static IE As Object, t1 As Double
  
  ' Cancel the repeating of this macro
  If Cancel Then
    ' Cancel schedule
    If t1 Then Application.OnTime t1, "LoopIE", Schedule:=False
    t1 = 0
    ' Reset IE object
    On Error Resume Next
    If Not IE Is Nothing Then IE.Quit
    Set IE = Nothing
    Exit Sub
  End If
  
  ' Trap error
  On Error GoTo exit_

  ' Main
  If IE Is Nothing Then Set IE = CreateObject("InternetExplorer.Application")
  With IE
    .Visible = True
    ' Disable pop-up msgs
    .Silent = True
    ' Navigate
    Application.StatusBar = "Navigating to: " & Link & " ..."
    .Navigate Link
    ' Charge the timeout
    t = Timer + WaitSeconds
    ' Wait for "IE is ready" state
    Application.StatusBar = "Waiting for IE's complete state..."
    While .readyState <> 4 And Timer < t: DoEvents: Wend
    ' Wait for "IE.Document is completely downloaded" state
    If Timer < t Then
      Application.StatusBar = "Waiting for Document's downloaded state..."
      While .Document Is Nothing And Timer < t: DoEvents: Wend
    Else
      Err.Raise vbObjectError + 513, , "Timeout happens: " & WaitSeconds & " seconds"
    End If
  End With

exit_:

  ' Inform about error
  If Err.Number <> 0 Then
    Application.StatusBar = "NavigateTo: " & Replace(Err.Description, vbLf, " - ")
    ' Show error message
    MsgBox Err.Description, vbExclamation, "Error"
    ' Reset IE object
    On Error Resume Next
    If Not IE Is Nothing Then IE.Quit
    Set IE = Nothing
  Else
    ' Clear StatusBar
    Application.StatusBar = False
    ' Charge schedule
    t1 = Now + TimeSerial(0, RepeatMinutes, 0)
    Application.OnTime t1, "LoopIE"
  End If

End Sub

' Macro for the Start button
Sub Start()
  LoopIE
End Sub

' Cancel schedule and quit IE
Sub Auto_Close()
  LoopIE Cancel:=True
End Sub

Regards



Hello,
I know it's been a while since this thread was active but I can't find the answer anywhere.

Here's what I've got:


VBA Function - Find specific internet explorer open tab
Hello there,


I'm building a macro to manage some tickets and I now want the application to get some data from a website.

1st I need it to sort out which tab to use, the only value that's unique is the ticket number, in this case "INC0347911"


I've seen a few places in the HTML code where the ticket number is located


Here's a chunk of the site

HTML:
******** type="text/ng-template" id="angular.do?sysparm_type=get_partial&name=sn_reference_popover_button.xml">
  <button ng-click="togglePopover($event)" tabindex="{{::buttonTabIndex}}" data-for="sys_display.{{ref}}" name="lookup.{{ref}}" id="lookup.{{ref}}" title="Lookup using list" type="button" class="btn btn-default sn-popover-complex icon icon-search"><span class="sr-only">Lookup using list</span></button>*********><div class="section_header_div_no_scroll form_title" data-form-title="INC0347911" id="incident.form_header"><input type="hidden" value="incident.do" id="section_form_id"></input><span tab_caption="Incident" id="section_tab.bf1d96e3c0a801640190725e63f8ac80" class="tabs2_section tabs2_section_0 tabs2_section0" tab_caption_raw="" data-header-only="true" data-section-id="bf1d96e3c0a801640190725e63f8ac80"><span class="section " id="section-bf1d96e3c0a801640190725e63f8ac80" data-header-only="true"><nav class="navbar navbar-default section_zero " data-id="bf1d96e3c0a801640190725e63f8ac80" data-type="section_head"><div class="container-fluid"><div class="navbar-header"><input type="HIDDEN" id="sysverb_back"></input><button title="Back" class="btn btn-default icon-chevron-left navbar-btn" *******="return gsftSubmit(this.previousSibling)"><span class="sr-only">Back</span></button><button title="Additional actions" class="btn btn-icon icon-menu navbar-btn" *******="contextShow(event, '1', 200, grabOffsetTop(this) + 20, grabOffsetLeft(this) + 24);event.cancelBubble=true;Event.stop(event);"><span class="sr-only">Additional actions</span></button><h2 draggable="true" ondragstart="event.dataTransfer.setData('text', Object.toJSON({ icon: 'form', url: ****************.href, title: g_form.getTitle(), table: g_form.getTableName() }))" class="form_header navbar-title navbar-title-twoline"><input type="HIDDEN" name="sysverb_view_change" id="sysverb_view_change"></input><div title="Current View: Default view (click me for other views)" class="pointerhand" *******="contextShow(event, 'labelMenu', -1, 0, 0);event.cancelBubble=true;"><div class="navbar-title-caption">Incident</div><div class="navbar-title-display-value">INC0347911</div></div></h2></div><div class="navbar-right"><span style="display: ;" id="section_head_right.bf1d96e3c0a801640190725e63f8ac80">********>NOW.streamLinkTarget = '';

Here are the two places I've found where that ticket number appears:

HTML:
class="section_header_div_no_scroll form_title" data-form-title="INC0347911" id="incident.form_header"
HTML:
class="navbar-title-caption">Incident</div><div class="navbar-title-display-value">INC0347911

So I guess my question is how to look into all the available tabs and look for that particular class, to make sure I'm modifying the right tab.
Could someone point me in the right direction? I've been brwosing the web and came up with many chunks of code but they usually need the name of the tab or the URL, but there's no way to pick it up through URL because the ticket numebr is not in it, so I can't make it choose one ticket from the other 20 that are open.
Help please?

Thank you



UPDATE:
What I need to do is for the code to search through the HTML of my open instances of internet explorer and look for those INC numbers. Say I have three tabs open, "INC001", "INC007" and "INC010" and I only want it to look for "INC007" and bring it upwards or just zero on it so the rest of the code works and the right ticket gets filled.
I don't know, could you please let me know if you know how to do this? or point me in the right direction? I think the problem may also be that I don't know what to ask for...

THANK YOU
 
Upvote 0
...
What I need to do is for the code to search through the HTML of my open instances of internet explorer and look for those INC numbers. Say I have three tabs open, "INC001", "INC007" and "INC010" and I only want it to look for "INC007" and bring it upwards or just zero on it so the rest of the code works and the right ticket gets filled.
Hi Nahuster,
As to your PM and the above post, try this code:
Rich (BB code):
Sub Main()
  Dim IE As Object, HtmlText As String
  HtmlText = "data-form-title=""INC0347911"""   ' <-- the search HTML string
  Set IE = GetIeByHtmlText(HtmlText)
  If IE Is Nothing Then
    MsgBox "HtmlText is not found in any open IE instances"
  Else
     MsgBox "HtmlText is found in the open IE instance with URL:" & vbLf & IE.LocationURL
  End If
End Sub
 
Function GetIeByHtmlText(HtmlText As String) As Object
' ZVI: 2017-03-18 https://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window-2.html#post4781479
  Dim w As Object
  Dim i As Long
  For Each w In CreateObject("Shell.Application").Windows
    If w.Name = "Windows Internet Explorer" Then
      i = InStr(1, w.Document.Body.innerHTML, HtmlText, vbTextCompare)
      If i > 0 Then
        Set GetIeByHtmlText = w
        Exit For
      End If
    End If
  Next
End Function
Regards
 
Last edited:
Upvote 0
Hi Vladimir,

Thank you for the above code which helps me to open web page in same tab instead of to open second window or tab.

I need your help to go next step. using your above code i would like to add the following steps :

1 - Copy Cell A2 (sheet name : Sheet1)

2 - Go to Opened tab "www......... [ using Valadimir code GetIE_LateBinding() which is working perfectly]

3
...
9
Hi Misbah,

As you have already mentioned in the point #2 the GetIE_LateBinding() is working which is good.
But all your other points are about parsing the web page which is not the subject of this thread.
It is better to create new thread(s) for those points.

Best Regards
 
Last edited:
Upvote 0
Hi Nahuster,
As to your PM and the above post, try this code:
Rich (BB code):
Sub Main()
  Dim IE As Object, HtmlText As String
  HtmlText = "data-form-title=""INC0347911"""   ' <-- the search HTML string
  Set IE = GetIeByHtmlText(HtmlText)
  If IE Is Nothing Then
    MsgBox "HtmlText is not found in any open IE instances"
  Else
     MsgBox "HtmlText is found in the open IE instance with URL:" & vbLf & IE.LocationURL
  End If
End Sub
 
Function GetIeByHtmlText(HtmlText As String) As Object
' ZVI: 2017-03-18 https://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window-2.html#post4781479
  Dim w As Object
  Dim i As Long
  For Each w In CreateObject("Shell.Application").Windows
    If w.Name = "Windows Internet Explorer" Then
      i = InStr(1, w.Document.Body.innerHTML, HtmlText, vbTextCompare)
      If i > 0 Then
        Set GetIeByHtmlText = w
        Exit For
      End If
    End If
  Next
End Function
Regards




Really? was it that simple? WOW, I was actually looking for that bit that searches within HTML but didn't know how to get it.
I can't try it here because I work on a VDA and can't use it from home but will let you know on Monday.
Anyway, THANK YOU VERY MUCH, I've been asking around and this is the first reply I get in weeks.
Again, thank you Vladimir

Cheers!
 
Upvote 0
Good Evening!
Vladimir's codes on page one worked well for me for weeks.
It used to open Microsoft Edge (Windows 10 Browser).
However, yesterday my program started to struggle.
So I tested the original codes from this thread and they do not work anymore on my computer.

Bilder-Upload - Kostenlos Fotos hochladen und ins Netz stellen
Bilder-Upload - Kostenlos Fotos hochladen und ins Netz stellen

Thank you for your help!
Good evening! :)
Sorry for too late incoming to your post – has not tracked this thread.
It seems that once opening & closing of IE11 will solve that issue.
Hope this will help someone with such a problem.
Best Regards
 
Upvote 0
Really? was it that simple? WOW, I was actually looking for that bit that searches within HTML but didn't know how to get it.
I can't try it here because I work on a VDA and can't use it from home but will let you know on Monday.
Anyway, THANK YOU VERY MUCH, I've been asking around and this is the first reply I get in weeks.
Again, thank you Vladimir

Cheers!
Wish you a lot of useful answers in this forum!
(y)
 
Upvote 0
Hi Nahuster,
As to your PM and the above post, try this code:
Rich (BB code):
Sub Main()
  Dim IE As Object, HtmlText As String
  HtmlText = "data-form-title=""INC0347911"""   ' <-- the search HTML string
  Set IE = GetIeByHtmlText(HtmlText)
  If IE Is Nothing Then
    MsgBox "HtmlText is not found in any open IE instances"
  Else
     MsgBox "HtmlText is found in the open IE instance with URL:" & vbLf & IE.LocationURL
  End If
End Sub
 
Function GetIeByHtmlText(HtmlText As String) As Object
' ZVI: 2017-03-18 https://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window-2.html#post4781479
  Dim w As Object
  Dim i As Long
  For Each w In CreateObject("Shell.Application").Windows
    If w.Name = "Windows Internet Explorer" Then
      i = InStr(1, w.Document.Body.innerHTML, HtmlText, vbTextCompare)
      If i > 0 Then
        Set GetIeByHtmlText = w
        Exit For
      End If
    End If
  Next
End Function
Regards


Hello Vladimir, sorry to bother you again.
I've tried that and couldn't make it work.
I've tried using the line as you put it:
HtmlText = "data-form-title=""INC0347911"""
I also tried with just the INC number and searching for a class...
This one:
class
="navbar-title-display-value">INC0357749<

But couldn't make it work.
Is there anything else to do? or do you think I'm doing something wrong?
I can't find where the issue is.

If I run the sub it gives me the negative MsgBox

If I try running the function it says :

IE window with this Title is not found: ""

Thank you

 
Upvote 0

Forum statistics

Threads
1,215,260
Messages
6,123,926
Members
449,135
Latest member
NickWBA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top