MS Word VBA提取超链接从选定的文字

0

的问题

我将不胜感激,如果有人可以帮我,并说明应该改变在代码如下(我找到它 www.extendoffice.com)来限制这个部分选择的案文(不到工作,整个文件)。
代码是提取超链接从一个字doc到另一个。

Sub HyperlinksExtract()
    Dim oLink As Hyperlink
    Dim docCurrent As Document 'current document
    Dim docNew As Document 'new document
    Dim rngStory As StoryRanges
    Set docCurrent = ActiveDocument
    Set docNew = Documents.Add
    For Each oLink In docCurrent.Hyperlinks
        oLink.range.Copy
        docNew.Activate
        Selection.Paste
        Selection.TypeParagraph
    Next
     
    Set docNew = Nothing
    Set docCurrent = Nothing
End Sub
hyperlink ms-word selection vba
2021-10-29 14:38:25
1

最好的答案

0

诀窍就是商店的选择超链接在一个变量 selectedHyperlinks.

而且我总是尽量避免复制、粘贴。因此,我使用 Hyperlinks.Add 方法插入链接到新的文档

Sub HyperlinksExtract()

    Dim docCurrent As Document
    Dim docNew As Document
    
    Set docNew = Documents.Add
    Dim rgTarget As Range: Set rgTarget = docNew.Range
    
    Dim selectedHyperlinks As Hyperlinks
    Set selectedHyperlinks = Selection.Hyperlinks   '<<< this is where the selected hyperlinks are stored in the variable
    
    Dim oLink As Hyperlink
    
    For Each oLink In selectedHyperlinks 
        rgTarget.Collapse wdCollapseEnd
        docNew.Hyperlinks.Add rgTarget, oLink.Address, oLink.SubAddress, , oLink.TextToDisplay
        rgTarget.Move wdParagraph, 1
        rgTarget.InsertParagraphAfter
    Next
     
End Sub

2021-11-04 18:41:52

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................