【问题标题】:How to code vba to open internet explorer in new session?如何编写 vba 代码以在新会话中打开 Internet Explorer?
【发布时间】:2012-12-20 11:55:42
【问题描述】:

几个月以来我一直在努力完成这项工作,如何编写 VBA 代码以在新会话中打开 Internet Explorer 我有一个包含许多登录名的应用程序我需要使用自动化同时打开它们,我已经使用过

  set ie=new InternetExplorer  

但它会在旧会话中打开 ie,我想为每个登录打开新会话,请帮助我,我搜索了很多,但最终没有任何解决方案。 这是我的代码

 Function GetIE() As InternetExplorer

  Dim WScript
Dim objShellWindows

 Set objShell = CreateObject("Shell.Application")
 Set objShellWindows = objShell.Windows
 Set WScript = CreateObject("WScript.Shell")


 Dim ieStarted
 ieStarted = False

  Dim ieError
  ieError = False

    Dim seconds
      seconds = 0

  While (Not ieStarted) And (Not ieError) And (seconds < 30)

If (Not objShellWindows Is Nothing) Then
    Dim objIE As InternetExplorer
    Dim IE


    For Each objIE In objShellWindows

        If (Not objIE Is Nothing) Then

            If IsObject(objIE.Document) Then
                Set IE = objIE.Document

                If VarType(IE) = 8 Then

                    If IE.Title = EmptyTitle Then
                        If Err.Number = 0 Then
                            IE.Write LoadingMessage

                            objIE.navigate Sheet1.Login.Text
                        ieStarted = True
                        Set GetIE = objIE


                      Else

                       MsgBox ErrorMessage
                            Err.Clear
                            ieError = True

                            Exit For
                        End If
                    End If
                End If
            End If
        End If

        Set IE = Nothing
        Set objIE = Nothing
    Next
End If

Application.Wait Now + TimeValue("00:00:1")
seconds = seconds + 1
Wend

 Set objShellWindows = Nothing
 Set objShell = Nothing



   End Function

使用此代码我可以打开浏览器,但遗憾的是我的网页正在打开的 Outlook 中打开,请帮助

【问题讨论】:

    标签: internet-explorer vba excel webautomation


    【解决方案1】:

    显然-nomerge 参数会阻止会话合并。

    Shell("iexplore.exe -nomerge http://www.yoursite.com")
    

    更新

    根据您的评论,您需要获取 IE 对象。你也许可以使用这个:

    Dim wshShell
    Set wshShell = WScript.CreateObject("WScript.Shell")
    
    wshShell.Run "iexplore -nomerge http://www.google.com"
    
    Dim objShell
    Set objShell = CreateObject("Shell.Application")
    
    Dim objShellWindows
    Set objShellWindows = objShell.Windows
    
    Dim i
    Dim ieObject
    For i = 0 To objShellWindows.Count - 1
        If InStr(objShellWindows.Item(i).FullName, "iexplore.exe") <> 0 Then
            Set ieObject = objShellWindows.Item(i)
            If VarType(ieObject.Document) = 8 Then
                MsgBox "Loaded " & ieObject.Document.Title
                Exit For
            End If
        End If
    Next
    
    Set ieObject = Nothing
    Set objShellWindows = Nothing
    Set objShell = Nothing
    Set wshShell = Nothing
    

    【讨论】:

    • 这是在一个新会话中打开一个浏览器,但我无法获得 ie 对象,请检查我附加的代码。我需要具有“当我们从文件菜单打开新会话时”的功能
    • 你也许可以适应this answer。我会看看我是否可以简化它,如果可以的话,我会更新我的帖子。
    • 非常感谢您提供的链接中的代码运行良好,但我需要将主页留空才能正常工作:(
    • 用你给我的代码获取在这个 exlorer 之前已经打开的资源管理器的对象
    • @nothingisimpossible:不幸的是,我不是 VBA 程序员,我真的只是一起破解了这个。您可能必须通过 URL 搜索您的 IE 实例,或检查窗口属性以了解有关哪个窗口属于您的线索。
    【解决方案2】:

    使用 Excel 2010 - 这是我使用的命令按钮。将 google.com 替换为您要在其他浏览器中打开的网站。

    Private Sub commandname_Click()
    
    'Opens an Explorer with a web site 
    
    Dim IE As InternetExplorer
    
      Set IE = CreateObject("InternetExplorer.Application")
    
      IE.navigate ("http://WWW.GOOGLE.COM")
    
      IE.Visible = True
    
    End Sub
    

    【讨论】:

    • 您需要在Tools 中添加Microsoft Internet Controls > References 才能使用。
    • 或者使用Dim IE as Object继续使用后期绑定。
    【解决方案3】:

    This answer 更改后为我工作:

    Dim IE As InternetExplorer
    

    Dim IE As Object
    

    【讨论】:

    • 您需要在Tools 中添加Microsoft Internet Controls > References 才能使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    相关资源
    最近更新 更多