MS Word VBA Estrazione di collegamenti ipertestuali dal testo selezionato in word

0

Domanda

Sarei grato se qualcuno mi potrebbe aiutare e indicare ciò che deve essere cambiato nel codice riportato di seguito (l'ho trovato su www.extendoffice.comper limitare questo per la parte selezionata del testo (non per lavoro in tutto il documento).
Il codice è l'estrazione di collegamenti ipertestuali da un doc di word che di un altro.

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

Migliore risposta

0

Il trucco è quello di memorizzare selezionato i collegamenti ipertestuali in una variabile selectedHyperlinks.

Inoltre cerco sempre di evitare il copia/incolla.Quindi io sto usando la Hyperlinks.Add metodo per inserire il link al nuovo documento

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

In altre lingue

Questa pagina è in altre lingue

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