MS Word VBA Usuwanie hiperłączy z zaznaczonego tekstu w programie word

0

Pytanie

Byłbym wdzięczny, gdyby ktoś mógł mi pomóc i wskazać, co należy zmienić w poniższym kodzie (znalazłem go na www.extendoffice.com) ograniczyć to wydzieloną częścią tekstu (nie działa w całym dokumencie).
Kod pobiera hiperłącza z jednego dokumentu programu word do innego.

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

Najlepsza odpowiedź

0

Sztuką jest, aby zapisać wybrane hiperłącza w zmiennej selectedHyperlinks.

Poza tym, ja zawsze staram się unikać kopiowania/wklejania.Dlatego używam Hyperlinks.Add sposób wstawiania linków na nowy dokument

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

W innych językach

Ta strona jest w innych językach

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