简体   繁体   English

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

[英]How to navigate to an already opened IE window using vba?

So I've been trying to navigate to an already opened IE window using VBA. 所以我一直在尝试使用VBA导航到已打开的IE窗口。 I've found a post explaining what seems to be how to do this. 我发现了一篇帖子,解释了如何做到这一点。 However, my main problem now is that I'm not too familiar with VBA and my website in particular, doesn't seem to have a document title. 但是,我现在的主要问题是我不太熟悉VBA,特别是我的网站,似乎没有文档标题。 All I have in the html code when i inspect it in the Debugger is the following: 当我在调试器中检查它时,我在html代码中的所有内容如下:

<title></title>

Am I missing something here? 我在这里错过了什么吗? Is there any other way to refer to the website that may be easier or simpler? 有没有其他方式来引用可能更简单或更简单的网站?

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 code to interact with specific IE window that is already open VBA代码与已经打开的特定IE窗口进行交互

This is the way I normally handle said problem, 这是我通常处理上述问题的方式,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM