1
votes

I want to add many hyperlinks via VBA for my MS-Word file, for 1st paragraph the hyperlink is "./index/1.doc", 2nd paragraph is "./index/2.doc", and so on. The simpilfied procedure is 1)select one paragraph 2)add hyperlink, just as the following code says. However, the VBA code gives every paragraph same hyperlink which only should be for the last paragraph. So, is there any way to deselect in VBA to perform this case? Thanks

btw, the VBA can be tested on any MS-Word file with more than 1 paragraphs.

Sub addHypertext()
    For countParagraph = 1 To ActiveDocument.Paragraphs.Count
        Selection.Collapse Direction:=wdCollapseEnd    
        set para = ActiveDocument.Paragraphs(countParagraph)
        para.Select
        Set paraStyle = para.Style 
        ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, \
            Address:="./index/" & countParagraph & ".doc"    

        Rem this does not work 
        Rem Selection.Range.Style = paraStyle 

        Rem this does not work 
        Selection.Style = paraStyle 

        Rem this does not work too 
        Rem para.Style = paraStyle 

        Rem this produces "run-time error" 
        Rem para.Range.Style = "text"
    Next
End Sub
1

1 Answers

1
votes

I see what's happening. It is highlighting the paragraph tag and making it part of the hyperlink. I am curious if maybe you might just want to add a reference at the end of each paragraph instead. See if this is something you might want to try:

Sub addHypertext()
    Dim para As Paragraph

    For countParagraph = 1 To ActiveDocument.Paragraphs.Count
        Selection.Collapse Direction:=wdCollapseEnd
        Set para = ActiveDocument.Paragraphs(countParagraph)
        para.Range.Select
        Selection.MoveRight 1
        Selection.MoveLeft 1
        Selection.Font.Superscript = True
        Selection.TypeText "[" + Trim(Str(countParagraph)) + "]"
        Selection.MoveLeft Count:=Len("[" + Trim(Str(countParagraph)) + "]"), Extend:=wdExtend

        para.Range.Hyperlinks.Add Anchor:=Selection.Range, _
            Address:="./index/" & countParagraph & ".doc"

        Selection.Font.Superscript = False
    Next

End Sub

The above will put a reference link at the end of each paragraph like wikipedia does on their site.

The following will get the whole paragraph as the link as it looks like you initially wanted:

Sub addHypertext()
    Dim para As Paragraph

    For countParagraph = 1 To ActiveDocument.Paragraphs.Count
        Selection.Collapse Direction:=wdCollapseEnd
        Set para = ActiveDocument.Paragraphs(countParagraph)
        para.Range.Select
        Selection.MoveEnd Count:=-1

        para.Range.Hyperlinks.Add Anchor:=Selection.Range, _
            Address:="./index/" & countParagraph & ".doc"

    Next

End Sub

EDIT

To answer your addition as to styles, the following still keeps the link and changes the style.

Sub addHypertext()
    Dim para As Paragraph

    For countParagraph = 1 To ActiveDocument.Paragraphs.Count
        Selection.Collapse Direction:=wdCollapseEnd
        Set para = ActiveDocument.Paragraphs(countParagraph)
        para.Range.Select
        Selection.MoveEnd Count:=-1

        para.Range.Hyperlinks.Add Anchor:=Selection.Range, _
            Address:="./index/" & countParagraph & ".doc"

        para.Range.Select
        Selection.MoveEnd Count:=-1

        'Per your comments below for future visitors
        Selection.Style = "Normal"
        Selection.Style = "My Custom Style"

    Next

End Sub