dcsimg
Results 1 to 6 of 6

Thread: How do I Differentiate between instances of Internet explorer using VBA in Excel.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    70

    How do I Differentiate between instances of Internet explorer using VBA in Excel.

    If I have two or more instances of Internet Explorer open, what VBA code can I use in Excel to be able to close a specific one?
    ie.quit closes all of them even ones that have nothing to do with the excel program.
    I can only imagine that they already have ID codes, how do I manipulate it?

    I hope this working code is enough to show what I am doing:

    Code:
        Dim obj As Object: Dim ie As Object
        Set obj = CreateObject("shell.application")
        For Each ie In obj.Windows
        On Error GoTo ForgetMe
                If TypeName(ie.Document) = "HTMLDocument" Then
                        ie.Quit
                     Exit For
                End If
    ForgetMe:
        Next

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,729

    Re: How do I Differentiate between instances of Internet explorer using VBA in Excel.

    you would need some criteria as to which internet explorer windows are the ones you want to close, the existing code only differentiates between internet explorer and explorer windows, which you probably do not need to do

    as you have exit for only the first instance of ie should close, even with multiple tabs only one tab should close (tested)
    all tabs in the same window have the same hwnd, but different windows or sessions will have different hwnds, though i doubt you can use the hwnd anyway, you need some unique criteria, possibly the url (locationurl) or title (locationname) of the window



    i have noted that in windows 10, i can not rely on all explorer /internet explorer windows being returned by the shell object, and that some of the windows returned do do not allow automation
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    70

    Re: How do I Differentiate between instances of Internet explorer using VBA in Excel.

    I am going to have to do more testing but it does look as if all IE windows are shut down and not only the ones that are created by my program. At the moment, this is not a serious problem in that I use other browsers when this program is running (Weekends only).

    Still, I would like to explore how Windows handles this. There must be some sort of ID involved since I can select the images from the task bar when several instances of IE are open.

    Curiouser and curiouser....

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,729

    Re: How do I Differentiate between instances of Internet explorer using VBA in Excel.

    i tested both in windows 10 and xp, before posting, only one window or tab was closed when the code runs
    obviously, if the code runs multiple times, eventually all windows will be closed

    if you are opening an explorer window from excel, you should be able keep that explorer object in scope, then close that specific explorer window whenever you want
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,435

    Re: How do I Differentiate between instances of Internet explorer using VBA in Excel.

    Quote Originally Posted by GarySut View Post
    Still, I would like to explore how Windows handles this. There must be some sort of ID involved since I can select the images from the task bar when several instances of IE are open.

    Curiouser and curiouser....
    If you're using Shell/ShellExecute you receive the ProcessID as a Result from this function-call.
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    I say you're crazy not to!
    --------------------------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2017
    Posts
    70

    Re: How do I Differentiate between instances of Internet explorer using VBA in Excel.

    I appreciate your response but this has proven to be more complicateder than I can handle at the moment. As a result, I have solved the problem in a different way. To explain what I did would require an in depth discussion of the project itself and I doubt it would be worth the effort. If anyone is still interested, let me know.

    Mischief Managed.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width