vba 在 Visual Basic 中使用 IE 浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9719015/
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
Using an IE browser with Visual Basic
提问by paj
Struggling to find a solution to this one. From Visual Basic (VBA in Excel more specifically) I'm able to call an Internet Explorer window by title using
正在努力寻找解决此问题的方法。从 Visual Basic(更具体地说是 Excel 中的 VBA),我可以使用标题调用 Internet Explorer 窗口
AppActivate ("My Page Title - Windows Internet Explorer")
And it works great each time.
而且每次都很好用。
I can open a new window and send a url to it using..
我可以打开一个新窗口并使用..向它发送一个网址。
Dim ie As Object
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://websiteurl"
And that also works okay But it opens a new browser each time and I want it to always calls the same window.
这也可以正常工作但它每次都会打开一个新的浏览器,我希望它始终调用同一个窗口。
So can i Set ie
to equal the same page each time. So instead of
那么我可以设置ie
为每次都等于相同的页面吗?所以代替
Set ie = New InternetExplorer
It does something like
它做类似的事情
Set ie = ACTIVE InternetExplorer
(though that doesn't seem to exist).
Is there some way of setting ie
to be the same as AppActivate ("My Page Title - Internet Explorer")
?
(虽然这似乎不存在)。是否有某种设置ie
方式与 相同AppActivate ("My Page Title - Internet Explorer")
?
Thanks
谢谢
Full Code here:
完整代码在这里:
Sub Find_Recordings()
Dim MyAppID, ReturnValue
AppActivate ("My Page Title - Windows Internet Explorer")
SendKeys ("^a")
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys ("^c")
Application.Wait (Now + TimeValue("0:00:01"))
AppActivate ("Microsoft Excel")
Sheets("DataSearcher").Select
Range("K1").Select
ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon: = False
Range("A1").Select
Dim ie As Object
Set ie = New InternetExplorer
ie.Visible = True ie.Navigate "http://wwwmyurl"
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
ie.Document.getElementById("searchdata1").Value = Range("J1")
ie.Document.getElementById("library").Value = "RECORDINGS"
ie.Document.searchform.Submit
End Sub
回答by brettdj
You could try something like this, drawing heavily on reusing Internet Explorer COM Automation Objectto identify an instance of IE with then specific web page active that you are looking for.
您可以尝试类似的方法,大量利用 Internet Explorer COM 自动化对象的重用来识别 IE 的实例,并且您正在寻找的特定网页处于活动状态。
ChangestrURL = "http://www.theage.com.au/"
to
更改strURL = "http://www.theage.com.au/"
为
"My Page Title - Windows Internet Explorer" as necessary
必要时“我的页面标题 - Windows Internet Explorer”
VBA
VBA
Sub Test()
Dim ShellApp As Object
Dim ShellWindows As Object
Dim IEObject As Object
Dim strURL As String
strURL = "http://www.theage.com.au/"
Set ShellApp = CreateObject("Shell.Application")
Set ShellWindows = ShellApp.Windows()
Dim i
For i = 0 To ShellWindows.Count - 1
If InStr(ShellWindows.Item(i).FullName, "iexplore.exe") <> 0 Then
If ShellWindows.Item(i).LocationURL = strURL Then
Set IEObject = ShellWindows.Item(i)
MsgBox "IE instance with " & strURL & " found"
Exit For
End If
End If
Next
End Sub