简体   繁体   English

如何导航到已经打开的Internet Explorer窗口? (VBA)

[英]How to navigate to an already opened internet explorer window? (VBA)

so I've been searching around the web a lot on how to do this and can't seem to find any concrete information on this. 因此我一直在网上搜索有关如何执行此操作的很多信息,但似乎找不到任何具体信息。 I've seen a lot of examples on how to open a new internet explorer window and navigate to a specific website. 我已经看到了很多有关如何打开新的Internet Explorer窗口并导航到特定网站的示例。 However, in my particular case, I want to be able to simply navigate to an already opened internet explorer window (it would be the only window/tab open of internet explorer). 但是,在我的特定情况下,我希望能够简单地导航到一个已经打开的Internet Explorer窗口(这将是Internet Explorer唯一打开的窗口/选项卡)。 The reason for this is because every time I open the website, I need to log in before being able to do anything. 这是因为每次打开网站时,我需要先登录才能进行任何操作。 I would then ideally like to paste some ID's into a search box and press enter (I should be able to find out how to do this part through searching online). 然后,理想情况下,我想将一些ID粘贴到搜索框中,然后按Enter键(我应该能够通过在线搜索来了解如何执行此操作)。

Here is what I've found so far, but I'm a little lost and not sure how I would apply this bit of code to work as I would like it to. 到目前为止,这是我找到的内容,但是我有点迷茫,并且不确定如何将这部分代码按自己的意愿应用。

Sub ExplorerTest()



Const myPageTitle As String = "Wikipedia"
Const myPageURL As String = "http://en.wikipedia.org/wiki/Main_Page"
Const mySearchForm As String = "searchform"
Const mySearchInput As String = "searchInput"
Const mySearchTerm As String = "Document Object Model"
Const myButton As String = "Go"
Dim myIE As SHDocVw.InternetExplorer


With myIE.Document.forms(mySearchForm)
    'enter search term in text field
    .elements(mySearchInput).Value = mySearchTerm
    'press button "Go"
    .elements(myButton).Click
  End With

End Sub

This does all browser windows both Internet Explorer and Windows Explorer 这会完成所有浏览器窗口,包括Internet Explorer和Windows Explorer

Window is a Internet Explorer Window object. Window是Internet Explorer的Window对象。

Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
    msgbox window.location
Next

Or if you are sure it's the only one open 或者,如果您确定这是唯一的开放时间

Set x = GetObject(,"InternetExplorer.Application")

I try to check your description and I find that you want to get object of already opened IE window and than try to automate it. 我尝试检查您的描述,发现您想要获取已打开的IE窗口的对象,然后尝试使其自动化。

You can try to refer example below may help you. 您可以尝试参考下面的示例,可能会对您有所帮助。

In this example, You can see that already opened page get searched using it's title. 在此示例中,您可以看到使用其标题搜索已打开的页面。 Than you can use it's object to automate that page. 比起您可以使用它的对象来自动化该页面。

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

Output: 输出:

在此处输入图片说明

Reference: 参考:

VBA code to interact with specific IE window that is already open VBA代码与已打开的特定IE窗口进行交互

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

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