vba 使用 Visual Basic 在 IE 中获取当前 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11158341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:22:57  来源:igfitidea点击:

Get Current URL in IE Using Visual Basic

internet-explorerurlvba

提问by Lloyd Banks

I am working with the Internet Explorer object in Visual Basic. Is there a way to copy the current URL IE is displaying so I can paste it elsewhere with my clipboard?

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

采纳答案by Gaffi

Assuming you already have the IE window identified, which itself is a little more complicated - I can elaborate on this if needed:

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

Dim ieIEWindow As SHDocVw.InternetExplorer
Dim sIEURL As String

'Set your IE Window

sIEURL = ieIEWindow.LocationURL

To get the IE window, you'll need to reference the Microsoft Internet Controlslibrary (ieframe.dll) in the VBA editor by going to Tools=>References...and selecting it from the list. If that item is not available, the .dll file for me is located at C:\Windows\System32\ieframe.dll.

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

Once the reference is set, you'll have access to the so-called SHDocVwlibrary in your code.

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

You can grab the IE window (assuming only one open) using the following (untested, modified/reduced from my own working code):

您可以使用以下内容(未经测试,修改/减少我自己的工作代码)获取 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

The above can also be modified to allow for a selection of multiple open IE windows.

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

回答by Siddharth Rout

Damn! This reminds me of my vb6 days :)

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

Ok here is what I have. If there are more than 1 IE windows then it will take the last active (Current) IE window else if there is only one window then it would take that.

好的,这就是我所拥有的。如果有 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

NOTE: I have not done any error handling. I am sure you can take care of that ;)

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