本文介绍了VBA从ProcessID创建IE对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在私有模式下与IE交互VBA,请使用以下代码:

To interact VBA with IE in Private mode, I use this code:

Dim sh As Object, oWin As Object
Dim wss As Object
Dim IE as Object

Set wss = CreateObject("WScript.Shell")
Set sh = CreateObject("Shell.Application")

wss.exec "%ProgramFiles%/Internet Explorer/iexplore.exe -private"
''You will probably need a delay here
Application.Wait (Now + TimeValue("00:00:05"))

For Each oWin In sh.Windows
    If TypeName(oWin.document) = "HTMLDocument" Then
        Set IE = oWin
        Exit For
    End If
Next

问题是上述代码首先作为对象而不是由.exec命令执行的对象而打开了IE窗口.

The problem is that above code get first opened IE window as object not one executed by .exec command.

所以,我认为最好改用下面的代码:

So, I think it's better I use below code instead:

lPID = Shell("C:\Program Files\Internet Explorer\iexplore.exe -private", vbNormalFocus)

但是我不知道如何使用进程ID(lPID)创建IE对象.

But I don't know how to create IE object using Process ID (lPID).

推荐答案

我找到了一个解决方案,并对其进行了测试.工作!

I found a solution and I tested it. Work!

Dim sh As Object, oWin As Object
Dim wss As Object
Dim IE as Object

Set wss = CreateObject("WScript.Shell")
Set sh = CreateObject("Shell.Application")

wss.exec "%ProgramFiles%/Internet Explorer/iexplore.exe -private"
''You will probably need a delay here
Application.Wait (Now + TimeValue("00:00:05"))

' Get last opened shell
Set IE = sh.Windows(sh.Windows.Count - 1)

这篇关于VBA从ProcessID创建IE对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 23:12