通过VBA获取现有的IE

我的目标是将数据从同一网站上的多个网页中截取到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 

我也尝试了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. 

我不想使用CreateObject或其他任何变体的原因是因为我已经准备好了可以被macros观抓取的标签了。 AppActivate与Microsoft Excel,但不是IE完美的工作。 我不能做如下确切的标题栏:

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

我不能使用这个原因是由于不同的命名页面的标签,不断改变标题。 我也试过:

  AppActivate InternetExplorer.Application . AppActivate InternetExplorer . AppActivate " - Microsoft Internet Explorer" . AppActivate "Microsoft Internet Explorer" 

上述所有的东西根本不能识别,或者仅仅闪烁几分之一秒(如第一个代码所述)。 我已经尝试了其他方法,但他们创build新的IE实例,当我想使用现有的IE打开多标签。 我也想说,在不同的代码:

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

工作完全正常,直到我改变它尝试引用IE浏览器,无论我在哪个页面上。 然后,旧的AppActivate代码将不再起作用。 我打破了。 我没有备份,真是愚蠢至极。

感谢您的协助! :) 谢谢。

我正在运行的系统:

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

我想到了! 感谢这个线程 ,以下工作:

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

无论命名scheme如何,都能提出任何网站。 当然,它看起来笨拙,但我不能抱怨,如果它的工作。

快乐的一天!

好的,所以我注意到使用Shell对象的第一个方法的潜在问题。

.FullName属性可能具有不正确的值。 (当我debugging和检查ShellWindows集合,我看到path""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 

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