通过 VBA 获取现有的 IE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25897956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get existing IE via VBA
提问by deafblind_TDK
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.
我的目标是将同一站点上多个网页的数据抓取到 Excel 中。我在 IE8 中的选项卡中打开了这些页面。我没有打开其他 IE 窗口。
I have tried the following to open 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
I also tried GetObject (I do not want to use the CreateObject method) as follows
我也试过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.
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:
我不想使用 CreateObject 或任何变体的原因是因为我已经打开了准备抓取的选项卡。AppActivate 适用于 Microsoft Excel,但不适用于 IE。我不能做确切的标题栏如下:
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"
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.
以上所有内容要么无法识别,要么仅闪烁几分之一秒(如第一个代码中所述)。我尝试过其他方法,但是当我想在打开的多选项卡中使用现有 IE 时,它们会创建一个新的 IE 实例。
In a different code:
在不同的代码中:
AppActivate "[name of page] - Microsoft Internet Explorer provided by [my company]"
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.
一直工作到我将其更改为尝试引用 IE,无论我在哪个页面上。那么旧的 AppActivate 代码将不再有效。我打破了它。没有备份我真是愚蠢。
System I am running:
我正在运行的系统:
Windows 7, IE8 (I'm waiting on my company to upgrade), Excel 2010
Windows 7、IE8(我正在等待我的公司升级)、Excel 2010
回答by ashleedawg
A compressed combination of the above answers, here's what works for me.
以上答案的压缩组合,这对我有用。
GetIEFunction
GetIE功能
(No references required.)
(不需要参考。)
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
Example Usage:
示例用法:
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
回答by Umesh Joshi
Made some changes and it works now
进行了一些更改,现在可以使用了
Function GetIE() As Object
Dim ShellApp As Object, ShellWindows As Object
Dim IEObject As Object
Set ShellApp = CreateObject("Shell.Application")
Set ShellWindows = ShellApp.Windows()
Dim item As Object
On Error GoTo 0
Dim sName As String
For Each ObjWind In ShellWindows
'On Error Resume Next
If (Not ObjWind Is Nothing) Then
sName = ObjWind.Name
If sName = "Internet Explorer" Then
Set IEObject = ObjWind
Exit For 'No need to continue....
End If
End If
Next
If IEObject Is Nothing Then Set IEObject = CreateObject("InternetExplorer.Application")
Set ShellApp = Nothing
Set GetIE = IEObject
End Function
回答by ejlj
Follow this link to see the code to use an open IE window. http://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window.html
按照此链接查看代码以使用打开的 IE 窗口。 http://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window.html
回答by David Zemens
OK, so I notice a potential problem with the first method you attempt using the Shell object.
好的,所以我注意到您尝试使用 Shell 对象的第一种方法存在潜在问题。
You may have an incorrect value for the .FullNameproperty. (when I debug and examin the ShellWindowscollection, I see path ""C:\Program Files (x86)\Internet Explorer\"
您的.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
You can put this function in your code, just tuck it away somewhere, then whenever you need IE you can call it like:
你可以把这个函数放在你的代码中,把它藏在某个地方,然后当你需要 IE 时,你可以像这样调用它:
回答by deafblind_TDK
I figured it out! Thanks to this thread, the following works:
我想到了!感谢这个线程,以下工作:
AppActivate " - Microsoft Internet Explorer provided by [my company]"
AppActivate " - Microsoft Internet Explorer provided by [my company]"
And it is able to bring forward any site, no matter the naming scheme. Granted, it looks clumsy, but I can't complain if it works.
无论命名方案如何,它都可以提出任何站点。当然,它看起来很笨拙,但如果它有效,我不能抱怨。
Happy day!
愉快的一天!

