Answered Activate Internet Explorer

  • 8 mai 2012 12:26
     
     

    Using Office 2007.

    Could someone please post generic example code illustrating how to activate IE using VBA?

    I would like to code the following two scenarios:

    When the code is run MS-Excel is active,

    If IE is running, then just activate IE, and stay on the active web page...

    If IE is not running, then load the app and activate a certain web page...

    Thanks!

Toate mesajele

  • 8 mai 2012 22:21
     
     Răspuns
    Sub ie_open()
        Dim ie As Object
        Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
        ie.NAVIGATE "http://www.google.com"
        ie.Visible = True
    End Sub
    • Propus ca răspuns de Brett N 13 8 mai 2012 22:25
    • Marcat ca răspuns de XP3 9 mai 2012 11:33
    •  
  • 10 mai 2012 09:07
    Moderator
     
      Are cod

    Hi XP3,

    To get the running IE, you can take a look at the following code:

    Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
       (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    
    Public Function IENavigate(ByRef IEBrowser) As Boolean
    
    Dim theSHD As SHDocVw.ShellWindows
    Dim IE As SHDocVw.InternetExplorer
    Dim i As Long
    Dim bIEFound As Boolean
    
    On Error GoTo Err_IE
    
        Set theSHD = New SHDocVw.ShellWindows
        For i = 0 To theSHD.Count - 1
            Set IE = theSHD.Item(i)
            If Not IE Is Nothing Then
            
                    If IE.Visible = True Then bIEFound = True: Exit For
                    
                   
            End If
        Next
    
        If bIEFound = True Then
            Set IEBrowser = IE
            IENavigate = True
        Else
            IENavigate = False
        End If
        
    ' Error Handling
    Err_IE:
       If Err <> 0 Then
            Err.Clear
            Resume Next
       End If
       
       Set theSHD = Nothing
    
    End Function
    
    Sub GetIE()
    
      Dim IEBrowser As InternetExplorer
      IENavigate IEBrowser
      If Not IEBrowser Is Nothing Then
        IEBrowser.Visible = True
        
       Else
        Set IEBrowser = New InternetExplorer
        IEBrowser.Visible = True
        IEBrowser.Navigate ("www.microsoft.com")
        
      End If
            
      apiShowWindow IEBrowser.hwnd, 3
     
    End Sub

    Notice: The solution refers to "Microsoft Internet Controls" library

    Reference from: http://vbadud.blogspot.com/2010/07/getobject-error-with-internet-explorer.html

    Thanks.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us