使用Visual Basic的IE浏览器

时间:2012-03-15 11:45:30

标签: excel internet-explorer vba excel-vba shdocvw

努力寻找解决方案。 从Visual Basic(更具体地说是Excel中的VBA)我可以使用

按标题调用Internet Explorer窗口
AppActivate ("My Page Title - Windows Internet Explorer")

每次都很棒。

我可以打开一个新窗口并使用..

发送一个网址
Dim ie As Object
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "http://websiteurl"

这也行得通 但它每次都会打开一个新的浏览器,我希望它总是调用同一个窗口。

我可以每次将ie设置为相同的页面。而不是

Set ie = New InternetExplorer

它有点像

Set ie = ACTIVE InternetExplorer

(虽然这似乎不存在)。 是否有某种方法可以将ie设置为与AppActivate ("My Page Title - Internet Explorer")相同?

由于

完整代码:

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

1 个答案:

答案 0 :(得分:1)

您可以尝试类似这样的事情,大量使用reusing Internet Explorer COM Automation Object来识别IE的实例,然后将您正在寻找的特定网页处于活动状态。

更改
strURL = "http://www.theage.com.au/"

  

“我的页面标题 - Windows Internet Explorer”   必要时

<强> 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