【问题标题】:Get Current URL in IE Using Visual Basic使用 Visual Basic 在 IE 中获取当前 URL
【发布时间】:2012-06-22 14:35:25
【问题描述】:

我正在使用 Visual Basic 中的 Internet Explorer 对象。有没有办法复制 IE 显示的当前 URL,以便我可以使用剪贴板将其粘贴到其他地方?

【问题讨论】:

  • 这可能比你想要的复杂一点,它是 VB.NET,但它应该是可翻译的。希望对你有帮助Get URL
  • 哪个应用程序? Excel、Access ..?
  • 你是如何与当前的IE连接的?这很重要,因为如果打开了 1 个以上的 IE 窗口怎么办。您是否根据标题与 IE 绑定?向我们展示更多代码实际上会有所帮助。如果您希望代码从所有 IE 窗口获取 URL,那么您可以设置对 Microsoft Internet Controls 的引用,然后您可以使用ShellWindows 遍历所有 IE 窗口。获取窗口后,只需使用.LocationURL 获取地址。

标签: internet-explorer url vba


【解决方案1】:

假设您已经识别了 IE 窗口,它本身有点复杂 - 如果需要,我可以详细说明:

Dim ieIEWindow As SHDocVw.InternetExplorer
Dim sIEURL As String

'Set your IE Window

sIEURL = ieIEWindow.LocationURL

要获得 IE 窗口,您需要在 VBA 编辑器中引用 Microsoft Internet Controls 库 (ieframe.dll),方法是转到 Tools=>References... 并从列表中选择它。如果该项目不可用,我的 .dll 文件位于 C:\Windows\System32\ieframe.dll

设置引用后,您将可以访问代码中所谓的SHDocVw 库。

您可以使用以下代码(未经测试、修改/减少自我自己的工作代码)抓取 IE 窗口(假设只打开一个):

Public Function GrabIEWindow() As SHDocView.InternetExplorer

Dim swShellWindows As New SHDocVw.ShellWindows
Dim ieOpenIEWindow As SHDocVw.InternetExplorer

    Set GrabIEWindow = Nothing

    ' Look at the URLs of any active Explorer windows 
    ' (this includes WINDOWS windows, not just IE)
    For Each ieOpenIEWindow In objShellWindows

        ' Check the I.E. window to see if it's pointed 
        ' to a web location (http)
        If Left$(ieOpenIEWindow.LocationURL, 4) = "http" Then
            ' If so, set this window as the one to use. 
            ' This will need to be modified to create
            ' a list if you want to select from more
            ' than one open window

            ' Optional grab the HWND for later reference...
            Dim lWindowID As Long
            lWindowID = ieOpenIEWindow.HWND

            Set GrabIEWindow = ieOpenIEWindow

            Exit Function
        End If

    Next OpenIEWindow 

End Function

以上内容也可以修改为允许选择多个打开的 IE 窗口。

【讨论】:

  • + 1 是的,如果只打开一个窗口或识别 IE 窗口。
  • @SiddharthRout 我确实每天都使用这种自动化,并开发了一些肮脏但有效的工具来实现这一点。 (列出所有可用窗口、用户选择、会话然后绑定到 HWND 的表单,在某些情况下确保窗口仍然打开等)
  • Kool :) 您可能想修改您的帖子并添加用户需要添加对 Microsoft Internet 控件的引用?
  • @SiddharthRout 也许,但我再次假设他们已经识别了窗口,这意味着他们应该已经有了...现在添加。
【解决方案2】:

该死!这让我想起了我的 vb6 天 :)

好的,这就是我所拥有的。如果有超过 1 个 IE 窗口,则它会占用最后一个活动(当前) IE 窗口,否则如果只有一个窗口,则它会占用最后一个窗口。

'~~> Set a reference to Microsoft Internet Controls

'~~> The GetWindow function retrieves the handle of a window that has
'~~> the specified relationship (Z order or owner) to the specified window.
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long

'~~> The GetForegroundWindow function returns the handle of the foreground
'~~> window (the window with which the user is currently working).
Private Declare Function GetForegroundWindow Lib "user32" () As Long

Sub GetURL()
    Dim sw As SHDocVw.ShellWindows
    Dim objIE As SHDocVw.InternetExplorer
    Dim topHwnd As Long, nextHwnd As Long
    Dim sURL As String, hwnds As String

    Set sw = New SHDocVw.ShellWindows

    '~~> Check the number of IE Windows Opened
    '~~> If more than 1
    hwnds = "|"
    If sw.Count > 1 Then
        '~~> Create a string of hwnds of all IE windows
        For Each objIE In sw
            hwnds = hwnds & objIE.hwnd & "|"
        Next

        '~~> Get handle of handle of the foreground window
        nextHwnd = GetForegroundWindow

        '~~> Check for the 1st IE window after foreground window
        Do While nextHwnd > 0
            nextHwnd = GetWindow(nextHwnd, 2&)
            If InStr(hwnds, "|" & nextHwnd & "|") > 0 Then
                topHwnd = nextHwnd
                Exit Do
            End If
        Loop

        '~~> Get the URL from the relevant IE window
        For Each objIE In sw
            If objIE.hwnd = topHwnd Then
                sURL = objIE.LocationURL
                Exit For
            End If
        Next
    '~~> If only 1 was found
    Else
        For Each objIE In sw
            sURL = objIE.LocationURL
        Next
    End If

    Debug.Print sURL

    Set sw = Nothing: Set objIE = Nothing
End Sub

注意:我没有做任何错误处理。我相信你可以解决这个问题;)

【讨论】:

  • 这和我的方法有点不同!我使用了一些外部 dll 引用,但仅用于操作页面(即在多个页面之间来回跳转)。当唯一的窗口具有非 http 路径时,您如何处理 sw.Count = 1,因为窗口可能是标准的 Windows 框架(即My Computer)?
  • 就像我提到的,我没有包含任何错误处理。像你所做的那样使用Left$(ieOpenIEWindow.LocationURL, 4) = "http" 将是我的下一步,但我正在使用 3 个具有实际地址的有效 IE 窗口进行测试。 :) 尝试使用超过 1 个 IE 窗口,看看你得到了什么 URL?
猜你喜欢
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
相关资源
最近更新 更多