如何在新会话中对VBA进行编码以打开Internet Explorer?

not*_*ble 3 excel internet-explorer vba excel-vba webautomation

几个月以来,我一直在努力完成此工作,如何在新会话中使用VBA编码以打开Internet Explorer我有一个具有许多登录名的应用程序,我需要使用自动化功能同时打开它们,

  set ie=new InternetExplorer  
Run Code Online (Sandbox Code Playgroud)

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

 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
Run Code Online (Sandbox Code Playgroud)

这段代码无法打开浏览器,但可惜我的网页正在Outlook中打开,已经打开了,请帮忙

Cᴏʀ*_*ᴏʀʏ 6

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

Shell("iexplore.exe -nomerge http://www.yoursite.com")
Run Code Online (Sandbox Code Playgroud)

更新

根据您的评论,您需要获取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
Run Code Online (Sandbox Code Playgroud)


小智 5

使用 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
Run Code Online (Sandbox Code Playgroud)