如何编写vba以在新会话中打开Internet Explorer? [英] How to code vba to open internet explorer in new session?

查看:259
本文介绍了如何编写vba以在新会话中打开Internet Explorer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  set ie = new InternetExplorer 

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

 函数GetIE()作为InternetExplorer 

Dim WScript
Dim objShellWindows

设置objShell = CreateObject(Shell.Application)
设置objShellWindows = objShell.Windows
设置WScript = CreateObject(WScript.Shell)


Dim ieStarted
ieStarted = False

Dim ieError
ieError = False

Dim seconds
秒= 0

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

If(Not objShellWindows is Nothing)Then
Dim objIE因为InternetExplorer
Dim IE


对于每个objIE在objShellWindows

如果(不是objIE是没有)然后

如果IsObject(objIE.Document)然后
设置IE = objIE.Document

如果VarType(IE)= 8然后

如果IE.Title = EmptyTitle然后
如果Err.Number = 0然后
IE.Write LoadingMessage

objIE.navigate Sheet1.Login.Text
ieStarted = True
设置GetIE = objIE


Else

MsgBox ErrorMessage
Err.Clear
ieError = True

退出
结束如果
结束如果
结束如果
结束如果
结束如果

设置IE =没有
设置objIE =没有
下一个
结束如果

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

设置objShellWindows =没有
设置objShell = Nothing



结束功能

这个代码可以打开浏览器,但可悲的是我的网页是开放的已经开放的展望pls help

解决方案

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

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

更新



根据您的评论,您需要获取IE对象。您可以使用以下工具:

  Dim wshShell 
设置wshShell = WScript.CreateObject(WScript.Shell )

wshShell.Runiexplore -nomerge http://www.google.com

Dim objShell
设置objShell = CreateObject(Shell.Application )

Dim objShellWindows
设置objShellWindows = objShell.Windows

Dim i
Dim ieObject
For i = 0 To objShellWindows.Count - 1
如果InStr(objShellWindows.Item(i).FullName,iexplore.exe)<> 0然后
设置ieObject = objShellWindows.Item(i)
如果VarType(ieObject.Document)= 8 Then
MsgBoxLoaded& ieObject.Document.Title
退出
结束If
结束If
下一个

设置ieObject = Nothing
设置objShellWindows = Nothing
设置objShell =没有
设置wshShell =没有


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

  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

 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

解决方案

Apparently the -nomerge argument will prevent session merging.

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:

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

这篇关于如何编写vba以在新会话中打开Internet Explorer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆