gpt4 book ai didi

excel - VBA从ProcessID创建IE对象

转载 作者:行者123 更新时间:2023-12-03 03:10:16 24 4
gpt4 key购买 nike

要在私有(private)模式下与 IE 交互 VBA,我使用以下代码:

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

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

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

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

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

最佳答案

#If Win64 Then
Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As LongPtr
#Else
Private Declare Function GetForegroundWindow Lib "user32" () As Long
#End If

Function getPrivateIE() As Object
#If Win64 Then
Dim hWnd As LongPtr
#Else
Dim hWnd As Long
#End If

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

Application.Wait Now + TimeSerial(0, 0, 2)

hWnd = GetForegroundWindow()

Dim oWin As Object

For Each oWin In CreateObject("Shell.Application").Windows
If oWin.hWnd = hWnd Then
Set getPrivateIE = oWin
Debug.Print "Success"
Exit For
End If
Next

End Function

关于excel - VBA从ProcessID创建IE对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57316998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com