【问题标题】:How to navigate to an already opened internet explorer window? (VBA)如何导航到已打开的 Internet Explorer 窗口? (VBA)
【发布时间】:2019-03-12 18:49:53
【问题描述】:

所以我在网上搜索了很多关于如何执行此操作的信息,但似乎找不到任何具体信息。我看过很多关于如何打开新的 Internet Explorer 窗口并导航到特定网站的示例。但是,在我的特定情况下,我希望能够简单地导航到已经打开的 Internet Explorer 窗口(这将是 Internet Explorer 中唯一打开的窗口/选项卡)。这样做的原因是因为每次我打开网站时,我都需要先登录才能做任何事情。然后,理想情况下,我想将一些 ID 粘贴到搜索框中并按 Enter(我应该能够通过在线搜索了解如何执行此部分)。

这是我目前发现的,但我有点迷茫,不知道如何应用这段代码来按我的意愿工作。

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

【问题讨论】:

  • 您可以自动登录
  • 好吧,我明白了。但是,我不会是唯一使用该宏的人,因此出于安全原因,我也无法自动登录其他人。
  • Op 为同一问题创建了一个 [重复线程]。参考:stackoverflow.com/questions/55164307/…

标签: excel vba internet-explorer


【解决方案1】:

我尝试检查您的描述,发现您想要获取已打开的 IE 窗口的对象,而不是尝试自动化它。

您可以尝试参考下面的示例可能对您有所帮助。

在此示例中,您可以看到已打开的页面使用其标题进行搜索。您可以使用它的对象来自动化该页面。

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

输出:

参考:

VBA code to interact with specific IE window that is already open

【讨论】:

  • 您好,非常感谢您的回复!这似乎可行,但是,我的问题是我的网页似乎没有文档标题。我对 html 不太熟悉,所以我不确定 VBA 代码的“XYZ”部分应该输入什么。如果我检查网站中的元素,这就是我为“标题部分”找到的 .... 有什么想法吗?
【解决方案2】:

这适用于 Internet Explorer 和 Windows Explorer 的所有浏览器窗口

Window 是 Internet Explorer 窗口对象。

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

或者如果你确定它是唯一一个打开的

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

【讨论】:

  • 非常感谢您!我不太确定这完全是如何工作的。但是,每当我尝试运行代码时,它都会给我一个错误:对象不支持该属性或方法。知道为什么会这样,我确定我在这里遗漏了一些东西,或者需要输入一些我错过的可能完全明显的东西!
猜你喜欢
  • 2017-04-08
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多