2012-06-22 34 views
6

我正在使用Visual Basic中的Internet Explorer對象。有沒有辦法複製當前的URL IE顯示,所以我可以粘貼到我的剪貼板其他地方?在IE中使用Visual Basic獲取當前URL

+0

這可能比你想要的更復雜一點,它是VB.NET,但它應該是可翻譯的。希望它有助於[Get URL](http://www.codeproject.com/Articles/204929/Getting-current-browser-URL-with-VB-NET) –

+0

哪個應用程序? Excel,Access ..? – Fionnuala

+0

你如何與當前的IE連接?這很重要,因爲如果有多個IE窗口打開的話。你是基於標題綁定IE嗎?向我們展示更多代碼實際上會有幫助如果您希望代碼從所有IE窗口獲取URL,那麼您可以設置對Microsoft Internet控件的引用,然後使用'ShellWindows'循環瀏覽所有IE窗口。一旦你看到窗口,只需使用'.LocationURL'來獲取地址。 –

回答

6

假設你已經在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窗口。

+0

+ 1是如果只有一個窗口打開或IE窗口被識別。 –

+0

@SiddharthRout我每天都會使用這種自動化,並且已經開發出一些骯髒但有效的工具來完成這項工作。 (窗體列出了所有可用的窗口,用戶選擇,會話然後綁定到HWND,在某些情況下,確保窗口仍然打開,等等)。 – Gaffi

+0

Kool :)您可能想要修改您的文章並添加用戶需要添加參考Microsoft Internet控件? –

2

該死!這讓我想起我的vb6天:)

好吧,這是我有。如果IE窗口超過1個,那麼它將採用最後一個活動窗口(Current),否則如果只有一個窗口,那麼它將採用該窗口。

'~~> 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 

注意:我沒有做過任何錯誤處理。我相信你可以照顧;)

+0

這與我的方法有很大的不同!我使用一些外部dll引用,但僅用於操作頁面(即在多個之間來回跳轉)。由於窗口可能是一個標準的Windows窗口(即「我的電腦」),因此當唯一窗口具有非http路徑時,如何處理'sw.Count = 1'? – Gaffi

+0

就像我提到的,我沒有包含任何錯誤處理。使用'Left $(ieOpenIEWindow。LocationURL,4)=「http」'就像你所做的一樣,這將是我的下一步,但我正在測試3個有效的IE窗口與實際地址。 :)嘗試使用超過1個IE窗口並查看您獲得了哪個URL? –