使用Visual Basic在IE中获取当前URL [英] Get Current URL in IE Using Visual Basic
问题描述
我正在使用Visual Basic中的Internet Explorer对象。有没有办法复制IE显示的当前URL,所以我可以用剪贴板粘贴到其他地方?
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?
推荐答案
假设您已经确定了IE窗口,这本身就有点复杂了 - 如果需要,我可以详细说明:
Assuming you already have the IE window identified, which itself is a little more complicated - I can elaborate on this if needed:
Dim ieIEWindow As SHDocVw.InternetExplorer
Dim sIEURL As String
'Set your IE Window
sIEURL = ieIEWindow.LocationURL
要获取IE窗口,您需要引用 Microsoft Internet控件
库( ieframe.dll
)在VBA编辑器中转到工具
= > 引用...
并从列表中选择它。如果该项目不可用,我的.dll文件位于 C:\ Windows \ System32 \ieframe.dll
。
To get the IE window, you'll need to reference the Microsoft Internet Controls
library (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
.
一旦设置了引用,您就可以在代码中访问所谓的 SHDocVw
库。
Once the reference is set, you'll have access to the so-called SHDocVw
library in your code.
您可以使用以下内容(从我自己的工作代码中未经测试,修改/减少)获取IE窗口(假设只有一个打开):
You can grab the IE window (assuming only one open) using the following (untested, modified/reduced from my own working code):
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窗口。
The above can also be modified to allow for a selection of multiple open IE windows.
这篇关于使用Visual Basic在IE中获取当前URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!