VBA Excel and IE-VBForums
Results 1 to 6 of 6

Thread: VBA Excel and IE

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Location
    Surrey, UK
    Posts
    163

    Resolved VBA Excel and IE

    Ok, should be a quick one. I'm struggling to do something that should be obvious. Must be a lack of cafeine or something. Anyway, any help greatly appreciated!

    Below is some simple code in Excel. If it finds a tab in IE that contains google.co.uk, it opens a new tab next to it and loads vbforums.com. My problem is I want the IE window and the new tab to become active on my desktop once its been created. Seems so simple yet I can do it....! ....help...

    Code:
        
    Dim w As InternetExplorer
    
        For Each w In CreateObject("shell.Application").Windows
            If InStr(1, w.LocationURL, "google.co.uk") > 0 Then
                    
                    w.Navigate "http://www.vbforums.com", , "_blank"
                    Exit For
            End If
        Next w
    Last edited by strobinson1; Sep 6th, 2012 at 08:51 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,021

    Re: VBA Excel and IE

    you can try
    AppActivate w.document.Title
    though i am not sure which ie window it will bring to the fore

    when i tested your code the page opened in a new browser window, rather than a tab and automatically came to the front
    Last edited by westconn1; Sep 6th, 2012 at 06:02 AM.
    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
    Addicted Member
    Join Date
    Sep 2004
    Location
    Surrey, UK
    Posts
    163

    Re: VBA Excel and IE

    I'm working with Excel 2003 and IE 8. Might be behaving differently if you are using a later Excel/IE ?

    Could there be a better way for me find the open IE object and navigate through the tabs? I've managed to get the window to pop-up now by cheating and adding

    w.visible = false
    w.visible = true

    but the new tab is not the active tab

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,021

    Re: VBA Excel and IE

    IE.navigate2 "www.bing.com", 2048 '2048 create new tab
    from a similar thread
    http://www.vbforums.com/showthread.p...=shell+windows

    some other information including

    navOpenInNewWindow = 0x1,
    navNoHistory = 0x2,
    navNoReadFromCache = 0x4,
    navNoWriteToCache = 0x8,
    navAllowAutosearch = 0x10,
    navBrowserBar = 0x20,
    navHyperlink = 0x40,
    navEnforceRestricted = 0x80,
    navNewWindowsManaged = 0x0100,
    navUntrustedForDownload = 0x0200,
    navTrustedForActiveX = 0x0400,
    navOpenInNewTab = 0x0800,
    navOpenInBackgroundTab = 0x1000,
    navKeepWordWheelText = 0x2000,
    navVirtualTab = 0x4000,
    navBlockRedirectsXDomain = 0x8000,
    navOpenNewForegroundTab = 0x10000
    actually i use older version of excel, but i doubt that matters at all
    don't know what version of IE as i only ever use it for vb code testing, it is always wanting to update, bloody annoying
    Last edited by westconn1; Sep 6th, 2012 at 07:19 AM.
    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
    Fanatic Member
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,006

    Re: VBA Excel and IE

    Have you checked your IE-Properties for Tab-Navigation, if "Activate New Tab" (or whatever it's called in the english version. I'm using the german version) is checked?

    As for bringing it to your front: google for "visual basic bring window to top"
    In VBA it's 2-3 API-Calls

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Location
    Surrey, UK
    Posts
    163

    Resolved Re: VBA Excel and IE

    Got it to work now. Very minor changes. Still needed the visible tweak in there to make the whole window appear. Job done. Cheers!


    Code:
        Dim w As InternetExplorer
        
        For Each w In CreateObject("shell.Application").Windows
            If InStr(1, w.LocationURL, "https://www.google.co.uk") > 0 Then
                    
                    w.Navigate2 "http://www.vbforums.com", 2048
                    w.Visible = False
                    w.Visible = True
                    Exit For
            End If
        Next w

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

Survey posted by VBForums.