2017-08-11 111 views
1

我有一個單詞文檔,塞滿了遍佈整個文檔的代碼和URL。如何將URL文檔從word文檔複製到其他文檔?

我一直在試圖找出如何在這個word文檔中提取的所有網址並將它們粘貼到另一個文檔上?

的URL都與https://subdomain.domain.com開始在同一網站..

問題..我會需要它通常爲.jpg

我已經試着用搜索引擎,但所有結束完整的URL鏈接我發現解決方案可以提取超鏈接的URL。無法找到我的情況的解決方案,所以我希望你們可以幫忙!

+0

請標記答案 – AndriuZ

回答

0

這將解析文檔,並發現所有的URL對你的輸出數組新Document

Option Explicit 

Sub FindLinks() 
    Dim p As Paragraph 
    Dim vSplit As Variant 
    Dim nIndex As Integer 
    Dim sURLs() As String 
    ReDim sURLs(0) 

    ' find each URL and add it to an array 
    For Each p In ActiveDocument.Paragraphs 
    vSplit = Split(p.Range, " ") 
    For nIndex = 0 To UBound(vSplit) 
     If InStr(vSplit(nIndex), "https://stackoverflow.com") > 0 Then 
     ReDim Preserve sURLs(UBound(sURLs) + 1) 
     sURLs(UBound(sURLs)) = Replace(vSplit(nIndex), "src=", "") 
     End If 
    Next 
    Next 

    ' create a new document and output the array 
    Dim sURL As Variant 
    Documents.Add 
    For Each sURL In sURLs 
    Selection.TypeText sURL 
    Selection.TypeParagraph 
    Next 

End Sub 
+0

嗨布拉克斯感謝您的幫助..腳本運行良好,但每行輸出都是這樣的,「src =」https://subdomain.domain.com/wp-content/uploads /2017/05/1608-7a9fa682e3651b02.jpg?size=96184 &高度= 640 &寬度= 640 &哈希= 710eee30989aa7a871e3e682d4923e28" 「 反正我們,我可以只顯示從HTTPS URL中的輸出:到的最後一部分該網址 - 無論是JPG或JPEG? – Alvin

+0

我添加了一個'Replace'。 – braX

1

我編輯的代碼,以將結果發送到C:\ TEMP \ my_links.txt。您可以編輯代碼來更改目的地。

Public Sub GetUrls() 
    Dim r As Range 
    Dim outfile As String 

    outfile = "C:\temp\my_links.txt" 
    Open outfile For Output As #1 
    Set r = ActiveDocument.Range 
    r.Select 

    With Selection.Find 
     .ClearFormatting 
     .Text = "https://subdomain.domain.com/*.jpg" 
     .Forward = True 
     .Wrap = wdFindStop 
     .MatchWildcards = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
    End With 

    Do While Selection.Find.Execute 
     Write #1, Selection.Text 
    Loop 

    Close #1 
End Sub 

當我在一個測試文件運行它,我得到這個在輸出文件:

"https://subdomain.domain.com/res1/joe.jpg" 
"https://subdomain.domain.com/res2/cat.jpg" 

希望有所幫助。

+0

試圖運行代碼,但我認爲我有更多的數據比199.所以立即窗口停止顯示其餘。你能幫助修補那些代碼將結果打印在新的word文檔中的代碼嗎?我想這種方式199限制不會發生! – Alvin

+0

我編輯了將結果發送到文件的代碼。這應該解決超過199個鏈接。 – xidgel