+ Reply to Thread
Results 1 to 3 of 3

How to Show Existing IE Instance if Already Exists, Else Open One

  1. #1
    cole.simonson@starwoodvo.com
    Guest

    How to Show Existing IE Instance if Already Exists, Else Open One

    Hi folks,
    I'm trying to do the following:

    > Test to see if Internet Explorer already has an instance open to address "www.mypage.com", for instance.
    > If it is already open, then change focus to that existing window.
    > If it is not already open, then start a new instance of IE with that URL.


    I've got many years as a programmer, but new to this language. I did
    find the following code that looks close to what I want, but I get an
    error msg that reads: "User-defined type not defined" - referencing the

    variable "objSW As SHDocVw.ShellWindows"

    Here is the code I found:


    Dim objSW As SHDocVw.ShellWindows
    Dim objIE As SHDocVw.InternetExplorer
    Dim objDoc As Object


    Set objSW = New SHDocVw.ShellWindows
    If objSW.Count Then ' new
    For Each objIE In objSW
    If InStr(1, objIE.FullName, "mypage") Then
    Set objIE = objSW.Item
    fAppRunning = True
    Exit For
    Else
    Set objIE = CreateObject("InternetExplorer.Application")
    End If
    Next objIE
    Else ' new
    Set objIE = CreateObject("InternetExplorer.Application") ' new
    End If


    objIE.Visible = True


    objIE.Navigate "www.mypage.com"


    Set objDoc = Nothing
    Set objIE = Nothing
    Set objSW = Nothing


    THANK YOU in advance for your help. GREATLY APPRECIATED!
    Kind regards,
    Cole


  2. #2
    Chip Pearson
    Guest

    Re: How to Show Existing IE Instance if Already Exists, Else Open One

    You need to add a reference to the "Microsoft Internet Controls"
    library. In VBA, go to the Tools menu, and choose References. In
    that dialog, scroll down to "Microsoft Internet Controls" and put
    a check next to it. This is the DLL that contains the definitions
    of the objects you are trying to use.



    Dim IE As SHDocVw.InternetExplorer

    On Error Resume Next
    Set IE = GetObject(, "InternetExplorer.Application")
    Debug.Print Err.Description
    On Error GoTo 0

    If IE Is Nothing Then
    Set IE = CreateObject("InternetExplorer.Application")
    If IE Is Nothing Then
    Debug.Print "error getting IE"
    End If

    End If


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    <cole.simonson@starwoodvo.com> wrote in message
    news:1149891707.605547.260650@i39g2000cwa.googlegroups.com...
    > Hi folks,
    > I'm trying to do the following:
    >
    >> Test to see if Internet Explorer already has an instance open
    >> to address "www.mypage.com", for instance.
    >> If it is already open, then change focus to that existing
    >> window.
    >> If it is not already open, then start a new instance of IE
    >> with that URL.

    >
    > I've got many years as a programmer, but new to this language.
    > I did
    > find the following code that looks close to what I want, but I
    > get an
    > error msg that reads: "User-defined type not defined" -
    > referencing the
    >
    > variable "objSW As SHDocVw.ShellWindows"
    >
    > Here is the code I found:
    >
    >
    > Dim objSW As SHDocVw.ShellWindows
    > Dim objIE As SHDocVw.InternetExplorer
    > Dim objDoc As Object
    >
    >
    > Set objSW = New SHDocVw.ShellWindows
    > If objSW.Count Then ' new
    > For Each objIE In objSW
    > If InStr(1, objIE.FullName, "mypage") Then
    > Set objIE = objSW.Item
    > fAppRunning = True
    > Exit For
    > Else
    > Set objIE =
    > CreateObject("InternetExplorer.Application")
    > End If
    > Next objIE
    > Else ' new
    > Set objIE = CreateObject("InternetExplorer.Application")
    > ' new
    > End If
    >
    >
    > objIE.Visible = True
    >
    >
    > objIE.Navigate "www.mypage.com"
    >
    >
    > Set objDoc = Nothing
    > Set objIE = Nothing
    > Set objSW = Nothing
    >
    >
    > THANK YOU in advance for your help. GREATLY APPRECIATED!
    > Kind regards,
    > Cole
    >




  3. #3
    PaulD
    Guest

    Re: How to Show Existing IE Instance if Already Exists, Else Open One

    Set the reference like Chip said, then here is another version of code that
    should do what you want. Replace "Google" with the page you are using.

    Public Sub CheckIE()
    Dim objSW As SHDocVw.ShellWindows
    Dim objIE As SHDocVw.InternetExplorer
    Dim objDoc As Object
    Dim bAppRunning As Boolean

    'Set objSW = New SHDocVw.ShellWindows
    If objSW.Count Then ' new
    For Each objDoc In objSW
    If InStr(1, objDoc.LocationName, "Google") Then
    bAppRunning = True
    objDoc.Visible = True
    Exit For
    End If
    Next objDoc
    End If
    If bAppRunning = False Then
    Set objIE = CreateObject("InternetExplorer.Application") ' new
    objIE.Visible = True
    objIE.Navigate "www.google.com"
    End If

    Set objIE = Nothing
    Set objSW = Nothing
    End Sub

    Paul D

    <cole.simonson@starwoodvo.com> wrote in message
    news:1149891707.605547.260650@i39g2000cwa.googlegroups.com...
    : Hi folks,
    : I'm trying to do the following:
    :
    : > Test to see if Internet Explorer already has an instance open to address
    "www.mypage.com", for instance.
    : > If it is already open, then change focus to that existing window.
    : > If it is not already open, then start a new instance of IE with that
    URL.
    :
    : I've got many years as a programmer, but new to this language. I did
    : find the following code that looks close to what I want, but I get an
    : error msg that reads: "User-defined type not defined" - referencing the
    :
    : variable "objSW As SHDocVw.ShellWindows"
    :
    : Here is the code I found:
    :
    :
    : Dim objSW As SHDocVw.ShellWindows
    : Dim objIE As SHDocVw.InternetExplorer
    : Dim objDoc As Object
    :
    :
    : Set objSW = New SHDocVw.ShellWindows
    : If objSW.Count Then ' new
    : For Each objIE In objSW
    : If InStr(1, objIE.FullName, "mypage") Then
    : Set objIE = objSW.Item
    : fAppRunning = True
    : Exit For
    : Else
    : Set objIE = CreateObject("InternetExplorer.Application")
    : End If
    : Next objIE
    : Else ' new
    : Set objIE = CreateObject("InternetExplorer.Application") ' new
    : End If
    :
    :
    : objIE.Visible = True
    :
    :
    : objIE.Navigate "www.mypage.com"
    :
    :
    : Set objDoc = Nothing
    : Set objIE = Nothing
    : Set objSW = Nothing
    :
    :
    : THANK YOU in advance for your help. GREATLY APPRECIATED!
    : Kind regards,
    : Cole
    :



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1