2011-11-01 61 views
0

我已經編寫了一些簡單的代碼來搜索IMDB搜索頁面中的電影名稱並提交表單。如何捕獲當前的URL並將其放入Excel中。使用VBA複製URL並粘貼到Excel中

這是我用來搜索IMDB的代碼。

Sub IMDB_URL_Search() 
    Movie = "The Shawshank Redemption" 
    Range("IV1").Select 
    ActiveSheet.HyperLinks.Add Anchor:=Selection, Address:= _ 
     "http://www.imdb.com/find?s=all&q=" & Replace(Movie, " ", "+") _ 
     , TextToDisplay:="Link" 
    Range("IV1").Select 
    Selection.HyperLinks(1).Follow NewWindow:=False, AddHistory:=True 
End Sub 

感謝, 蘇雷什

回答

0

Excel中有一個內置的 '超鏈接' 類來處理這個問題。您只需創建一個超鏈接變量來存儲從「添加」功能返回的超鏈接。從那裏您可以引用您通過'地址'屬性指向的URL。

Sub IMDB_URL_Search() 

    Dim movie As String 
    Dim link As Hyperlink 

    movie = "The Shawshank Redemption" 

    Range("IV1").Select 

    Set link = ActiveSheet.Hyperlinks.Add(Anchor:=Selection, Address:= _ 
     "http://www.imdb.com/find?s=all&q=" & Replace(movie, " ", "+") _ 
     , TextToDisplay:="Link") 

    link.Follow NewWindow:=False, AddHistory:=True 

    'link.address will be the URL for this web link 
    MsgBox link.Address 
End Sub