如何使用vba导航到已打开的IE窗口?

时间:2019-03-14 13:53:30

标签: excel vba internet-explorer

所以我一直在尝试使用VBA导航到已经打开的IE窗口。 我找到了一篇帖子,解释了如何执行此操作。但是,我现在的主要问题是我对VBA不太熟悉,尤其是我的网站似乎没有文档标题。当我在调试器中检查它时,我在html代码中所拥有的全部是:

<title></title>

我在这里错过了什么吗?还有其他更简便的方式来引用该网站吗?

Sub demo()

Dim str_val As Object
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next
    my_url = objShell.Windows(x).document.Location
    my_title = objShell.Windows(x).document.Title

    If my_title Like "XYZ" & "*" Then
        Set IE = objShell.Windows(x)
        marker = 1
        Exit For
    Else
    End If
Next

If marker = 0 Then
 MsgBox ("A matching webpage was NOT found")

Else
    MsgBox ("A matching webpage was found")

    Set str_val = IE.document.getElementById("txtbox1")
    str_val.Value = "demo text"

End If
End Sub

Reference

VBA代码与已经打开的特定IE窗口进行交互

1 个答案:

答案 0 :(得分:0)

这是我通常处理上述问题的方式,

Sub GetURLs()

'   Uses MS HTML Controls
'   and MS Internet Controls

Dim s As SHDocVw.InternetExplorer
Dim sw As SHDocVw.ShellWindows
Dim d As MSHTML.HTMLDocument

Set sw = New SHDocVw.ShellWindows

For Each s In sw
    If TypeName(s) = "IWebBrowser2" And s.Name <> "Windows Explorer" Then
        Debug.Print s.Name, s.LocationURL
        Set d = s.Document
        Debug.Print d.getElementsByTagName("TD")(0).innerhtml
    End If
Next s

Set sw = Nothing
Set s = Nothing

End Sub