VisualBasicでInternetExplorerオブジェクトを使用しています。IEが表示している現在のURLをコピーして、クリップボードで他の場所に貼り付ける方法はありますか?
2 に答える
IEウィンドウがすでに識別されていると仮定すると、それ自体はもう少し複雑です。必要に応じて、これについて詳しく説明できます。
Dim ieIEWindow As SHDocVw.InternetExplorer
Dim sIEURL As String
'Set your IE Window
sIEURL = ieIEWindow.LocationURL
IEウィンドウを表示するには、VBAエディターでMicrosoft Internet Controls
ライブラリ( )を参照する必要があります。=>に移動し、リストからライブラリを選択します。そのアイテムが利用できない場合、私のための.dllファイルはにあります。ieframe.dll
Tools
References...
C:\Windows\System32\ieframe.dll
参照が設定されると、コード内のいわゆるSHDocVw
ライブラリにアクセスできるようになります。
以下を使用して、IEウィンドウを取得できます(開いているのは1つだけと想定)(テストされていない、自分の作業コードから変更/縮小):
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ウィンドウを選択できるようにすることもできます。
くそ!これは私のvb6日を思い出させます:)
これが私が持っているものです。複数のIEウィンドウがある場合は、最後にアクティブな(現在の)IEウィンドウが使用されます。それ以外の場合は、ウィンドウが1つしかない場合は、それが使用されます。
'~~> 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
注:エラー処理は行っていません。私はあなたがそれの世話をすることができると確信しています;)