通过VBA获取现有IE

dea*_*TDK 5 excel internet-explorer vba

我的目标是将同一网站上多个网页上的数据抓取到Excel中。我在IE8的标签页中打开了这些页面。我没有打开其他IE窗口。

我已经尝试了以下方法来打开IE:

AppActivate "Microsoft Internet Explorer provided by [my company]"
' It loses focus, the titlebar flashes for a fraction of a second 
' .. another code ..
Dim ShellApp
Set ShellApp = CreateObject("Shell.Application")
Dim ShellWindows
Set ShellWindows = ShellApp.Windows()
Dim i
For i = 0 To ShellWindows.Count - 1
    If InStr(ShellWindows.Item(i).FullName, "iexplore.exe") <> 0 Then
        Set IEObject = ShellWindows.Item(i) 
    End If
Next
' Did absolutely nothing, grabbed this code from another solution on stackoverflow
Run Code Online (Sandbox Code Playgroud)

我还尝试了GetObject(我不想使用CreateObject方法),如下所示

dim ie As InternetExplorer 'also tried As Object and other variation
set ie = GetObject(, "InternetExplorer.Application")
'However this is not workable due to security risks and Microsoft 
' disabled GetObject for IE by design. 
Run Code Online (Sandbox Code Playgroud)

我不想使用CreateObject或任何变体的原因是因为我已经打开了可供抓取的选项卡。AppActivate使用Microsoft Excel,但不支持IE。我无法按以下方式进行准确的标题栏:

AppActivate "Website name - name page - Microsoft Internet Explorer provided by [my company]"
Run Code Online (Sandbox Code Playgroud)

我之所以不能使用此选项,是因为标签中的命名页面不断变化,标题不断变化。我也尝试过:

AppActivate InternetExplorer.Application

AppActivate InternetExplorer

AppActivate " - Microsoft Internet Explorer"

AppActivate "Microsoft Internet Explorer"
Run Code Online (Sandbox Code Playgroud)

以上所有这些都不被识别,或者只是闪烁了不到一秒钟(如第一条代码中所述)。我尝试了其他方法,但是当我想使用已打开的多选项卡的现有IE时,它们会创建一个新的IE实例。

用不同的代码:

AppActivate "[name of page] - Microsoft Internet Explorer provided by [my company]"
Run Code Online (Sandbox Code Playgroud)

一直工作到我更改它以尝试引用IE,无论我在哪个页面上。然后,旧的AppActivate代码将不再起作用。我弄坏了 没有备份对我来说是愚蠢的。

我正在运行的系统:

Windows 7,IE8(我正在等待公司升级),Excel 2010

Dav*_*ens 0

好的,我注意到您尝试使用 Shell 对象的第一个方法存在潜在问题。

您的财产价值可能不正确.FullName。(当我调试和检查ShellWindows集合时,我看到路径""C:\Program Files (x86)\Internet Explorer\"

Function GetIE() As Object

Dim ShellApp as Object, ShellWindows as Object, i as Long 
Dim IEObject As Object

Set ShellApp = CreateObject("Shell.Application")
Set ShellWindows = ShellApp.Windows()

For i = 0 To ShellWindows.Count - 1
    If InStr(ShellWindows.Item(i).FullName, "C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE") <> 0 Then
       Set IEObject = ShellWindows.Item(i)
       Exit For  'No need to continue....
    End If
Next

If IEObject Is Nothing Then Set IEObject = CreateObject("InternetExplorer.Application")

Set GetIE = IEObject

End Function
Run Code Online (Sandbox Code Playgroud)

你可以把这个函数放在你的代码中,只是把它藏在某个地方,然后每当你需要 IE 时你就可以像这样调用它: