如何编码 vba 以在新会话中打开 Internet Explorer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14184340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:41:49  来源:igfitidea点击:

How to code vba to open internet explorer in new session?

internet-explorervbaexcel-vbawebautomationexcel

提问by nothingisimpossible

I am struggling to get this done since months, how to code VBA to open internet explorer in new session i have an application with many logins i need to open them simultaneously using automation , i have used

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

  set ie=new InternetExplorer  

but it opens the ie within the old session, i want to open new session for each and every login please help me, i googled a lot for it but ended up with out any solution. this is my code

但它在旧会话中打开 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

with this code im able to open the browser but sadly my webpage is opening in outlook which is already opened pls help

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

回答by C???

Apparently the -nomergeargument will prevent session merging.

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

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

UPDATE

更新

As per your comment, you need to get the IE object. You may be able to work with this:

根据您的评论,您需要获取 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

回答by Melissa D

Using Excel 2010 - This is what I use with a command button. Replace google.com with the website you want to open in another browser.

使用 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

回答by hod

This answerworks for me after changing:

更改后,此答案对我有用:

Dim IE As InternetExplorer

to

Dim IE As Object