在Visual Basic中使用IE浏览器



正在努力寻找解决方案。从Visual Basic(更具体地说是Excel中的VBA),我可以使用按标题调用Internet Explorer窗口

AppActivate ("My Page Title - Windows Internet Explorer")

而且每次都很有效。

我可以打开一个新窗口,并使用..向其发送url。。

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

您可以尝试这样的方法,充分利用重用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

最新更新