通过VBA获取现有的IE [英] Get existing IE via VBA

查看:574
本文介绍了通过VBA获取现有的IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal is to scrape data off multiple webpages on the same site into Excel. I have these pages open in tabs within IE8. I have no other IE windows open.

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

I have tried the following to open 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

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

I also tried GetObject (I do not want to use the CreateObject method) as follows

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. 

我不想使用CreateObject或任何变体的原因是因为我已经打开选项卡准备好进行抓取了. AppActivate可与Microsoft Excel一起使用,但不适用于IE.我无法按照以下方式进行准确的标题栏:

The reason I don't want to use CreateObject or any of the variation is because I already have the tabs open ready to be scraped. AppActivate works with Microsoft Excel, but not IE. I cannot do exact titlebars as follows:

AppActivate "Website name - name page - Microsoft Internet Explorer provided by [my company]"

我无法使用此功能的原因是标签页的命名页面不断变化,标题不断变化.我也尝试过:

The reason I can't use this because of the tabs with varying named pages, changing the title constantly. I also tried:

AppActivate InternetExplorer.Application

AppActivate InternetExplorer

AppActivate " - Microsoft Internet Explorer"

AppActivate "Microsoft Internet Explorer"

以上所有内容要么未被识别,要么仅闪烁了不到一秒(如第一条代码所述).我尝试了其他方法,但是当我想使用已打开的多选项卡的现有IE时,它们会创建一个新的IE实例.

All of the above either are not recognized or merely flash for a fraction of a second (as mentioned in first code). I have tried other methods but they create a new IE instance when I want to use the existing IE with opened multi-tabs.

用不同的代码:

AppActivate "[name of page] - Microsoft Internet Explorer provided by [my company]"

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

worked until I changed it to try and reference IE, no matter what page I'm on. Then the old AppActivate code will no longer work. I broke it. It was dumb of me to not have a back up.

我正在运行的系统:

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

Windows 7, IE8 (I'm waiting on my company to upgrade), Excel 2010

推荐答案

以上答案的压缩组合,这对我有效.

A compressed combination of the above answers, here's what works for me.

(无需引用.)

Function GetIE() As Object
'return an object for the open Internet Explorer window, or create new one
  For Each GetIE In CreateObject("Shell.Application").Windows() 'Loop to find
    If (Not GetIE Is Nothing) And GetIE.Name = "Internet Explorer" Then Exit For 'Found!
  Next GetIE
  If GetIE Is Nothing Then Set GetIE=CreateObject("InternetExplorer.Application") 'Create
  GetIE.Visible = True 'Make IE window visible
End Function

用法示例:

Sub demo()
    Dim ie As Object
    Set ie = GetIE                                        'get new/existing IE object
    ie.Navigate "http://stackoverflow.com", 2             '2=don't keep history
    Do: DoEvents: Loop While ie.Busy or ie.ReadyState <> 4'wait til loaded
    Stop                                                  'do your stuff
    ie.Quit                                               'close IE
    Set ie = Nothing                                      'clean up
End Sub

这篇关于通过VBA获取现有的IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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