Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Internet explorer 使用Visual Basic获取IE中的当前URL_Internet Explorer_Url_Vba - Fatal编程技术网

Internet explorer 使用Visual Basic获取IE中的当前URL

Internet explorer 使用Visual Basic获取IE中的当前URL,internet-explorer,url,vba,Internet Explorer,Url,Vba,我正在使用Visual Basic中的Internet Explorer对象。有没有办法复制IE正在显示的当前URL,以便我可以将其粘贴到剪贴板的其他位置?假设您已经识别了IE窗口,它本身有点复杂-如果需要,我可以详细说明: 将IEWindow设置为SHDocVw.InternetExplorer 将URL设置为字符串 “把你的行李放在窗户上 sIEURL=iewindow.LocationURL 要获取IE窗口,您需要在VBA编辑器中通过转到Tools=>References…并从列表中选择

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

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

将IEWindow设置为SHDocVw.InternetExplorer
将URL设置为字符串
“把你的行李放在窗户上
sIEURL=iewindow.LocationURL
要获取IE窗口,您需要在VBA编辑器中通过转到
Tools
=>
References…
并从列表中选择来引用
Microsoft Internet控件库(
ieframe.dll
)。如果该项不可用,我的.dll文件位于
C:\Windows\System32\ieframe.dll

一旦设置了引用,您就可以访问代码中所谓的
SHDocVw

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

公共函数GrabIEWindow()作为SHDocView.InternetExplorer 将swShellWindows变暗为新SHDocVw.ShellWindows 将ieOpenIEWindow设置为SHDocVw.InternetExplorer 设置GrabIEWindow=Nothing '查看任何活动资源管理器窗口的URL (这包括WINDOWS,而不仅仅是IE) 对于objShellWindows中的每个ieOpenIEWindow '检查I.E.窗口是否指向 '到web位置(http) 如果左$(ieOpenIEWindow.LocationURL,4)=“http”,则 '如果是,请将此窗口设置为要使用的窗口。 '这需要修改才能创建 '如果要从更多列表中进行选择,请选择列表 “不止一扇开着的窗户 '可选抓取HWND供以后参考。。。 暗淡如长 lWindowID=ieOpenIEWindow.HWND 设置GrabIEWindow=ieOpenIEWindow 退出功能 如果结束 下一个OpenIEWindow 端函数

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

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

好的,这是我的。如果有多个IE窗口,则需要最后一个活动(当前)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

注意:我没有做任何错误处理。我相信你会处理好的;)

这可能比您想要的要复杂一些,它是VB.NET,但它应该是可翻译的。希望对哪个应用程序有帮助?Excel、Access..?您是如何与当前IE连接的?这很重要,因为如果打开的IE窗口超过1个。您是否根据标题与IE绑定?向我们展示更多的代码实际上会有所帮助。如果希望代码从所有IE窗口获取URL,则可以设置对Microsoft Internet控件的引用,然后可以使用
ShellWindows
在所有IE窗口中循环。一旦你得到了窗口,只需使用
.LocationURL
来获得地址。+1是的,如果只有一个窗口打开或者IE窗口被识别。@SiddharthRout我每天都使用这种自动化,并开发了一些肮脏但有效的工具来实现这一点。(列出所有可用窗口的表单,用户选择,会话然后绑定到HWND,在某些情况下确保窗口仍然打开,等等)Kool:)您可能想修改您的帖子,并添加用户需要添加对Microsoft Internet控件的引用?@SiddharthRout也许,但我再次假设他们已经识别了该窗口,这意味着他们应该已经有了。。。现在添加。这和我的方法有点不同!我使用一些外部dll引用,但仅用于操作页面(即在多个dll之间来回跳转)。当唯一的窗口具有非http路径时,您如何处理
sw.Count=1
,因为窗口可能是标准的Windows框架(即
My Computer
)?正如我提到的,我没有包含任何错误处理。我的下一步是像您那样使用
Left$(ieOpenIEWindow.LocationURL,4)=“http”
,但我使用3个具有实际地址的有效IE窗口进行测试:尝试使用多个IE窗口,看看你得到了什么URL?