2015-09-29 77 views
0

我發現很多不同的方案來解決這個問題,但這裏是我到目前爲止發現的最好的代碼:的不同選項卡打開多個超鏈接在VBA

Sub OpenHyperLinks() 
    Dim xHyperlink As Hyperlink 
    Dim WorkRng As Range 
    On Error Resume Next 
    xTitleId = "KutoolsforExcel" 
    Set WorkRng = Application.Selection 
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8) 
    For Each xHyperlink In WorkRng.Hyperlinks 
     xHyperlink.Follow 
    Next 
End Sub 

的問題是,這打開所有的超鏈接在同一個標​​籤中,以便我只加載最後一頁。我想要的是所有鏈接顯示在單獨的選項卡中。我如何修改這個以獲得我在找的東西?

+0

的[VBA,Excel 2010中,打開Internet在新標籤中探索]可能重複(http://stackoverflow.com/questions/17386091/vba-excel-2010-open-internet-explore-in-new -tab) –

+0

不要使用超鏈接'.Follow'方法,在瀏覽器(大概是默認的IE)上獲得一個明確的句柄,並使用其他答案中的方法將瀏覽器的'.Navigate'到'xHyperlink.Address'。 –

+0

我不知道爲什麼,但是當我在Firefox中打開一個新窗口時,嘗試再次運行代碼,它工作得很好,在新選項卡中打開每個超鏈接。所以實際上代碼工作正常。以前我試圖在已經有一些標籤打開的現有窗口中打開它 – xxxRxxx

回答

0

Welp,我終於找到了一個更好的解決方案:只需使用Wscript。

Sub OpenDemLinksBoi() 
    Dim SelecRng As Range 

    Set SelecRng = Application.Selection 
    Set SelecRng = Application.InputBox("Range", SelecRng, Type:=8) 

    For Each Cell In SelecRng 
    Set objShell = CreateObject("Wscript.Shell") 'I think this basically allows you to input shell commands, hence the "Run" in the next line, which functions exactly like Run. Test it out, try putting a link_ 
    'into Run and see what happens. Same thing as this script. 
    objShell.Run (Cell) 
    Next 

End Sub 
相關問題