Solved

Word VBA Hyperlinks

Posted on 2011-09-18
2
386 Views
Last Modified: 2012-05-12
Hi there,

I have the following code that loops through word document - word by word and based on criteria adds a hyperlink, the trouble is when adding the hyperlink it just stops on the same word and does not move onto the next word...how do I get it to move onto the next word?

thanks and regards

Sub setHyperLinkEntries()

previousxWord = ""
For Each xWord In ActiveDocument.Words

    If Len(xWord) = 6 Then
    
    If IsNumeric(Right(Left(xWord, 3), 1)) Then
    
        If Left(xWord, 2) = "HL" Then
            
            xWord.Select
            If Selection.Range.Text <> previousxWord Then
            
                ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
                    "https://sharepointsite/_layouts/WordViewer.aspx?id=/Shared%20Documents/" & Selection.Range.Text & ".docx" _
                    , SubAddress:="", ScreenTip:="", TextToDisplay:=Selection.Range.Text
                xWord.Select
                Selection.Move wdWord, 2
            End If
        
        Else
        End If
        
    Else
    End If
        
Else
End If

Next xWord

End Sub

Open in new window

Sample Text (you can use this to test the procedure)

2.2.6	Access rights are specified in section 11.1 below. Access rights are personal, are set out in individual User Agreements (see section 11.2 and 11.3 below) and cannot be delegated.
2.2.7	The authorization procedure for new information processing facilities is set out in HL0604.  
2.3	Org's ISMS documentation is protected and controlled. There is a documented procedure (HL0001) which takes 2.2 above into account and defines the management actions for document control. [ISO27001 4.3.2] 
2.4	Org has a documented procedure (HL0002) which defines the controls for identification, storage, protection, retrieval, retention time and disposal of records. [ISO27001 4.3.3] Documents are available to those who need and are authorized to access them in line with these retention requirements.

Open in new window

0
Comment
Question by:SMcP
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 36556423
The problem is that you change the Words collection when you add the hyperlink.

Try a wildcard find loop.
Sub sethyp()
Dim rng As Range
Dim hyp As Hyperlink

Set rng = ActiveDocument.Range
Do While True
    With rng.Find
        .MatchWildcards = True
        .Text = "HL[0-9]{4}"
        If .Execute() Then
            Set hyp = ActiveDocument.Hyperlinks.Add(Anchor:=rng, Address:= _
                "https://sharepointsite/_layouts/WordViewer.aspx?id=/Shared%20Documents/" & rng.Text & ".docx" _
                , SubAddress:="", ScreenTip:="", TextToDisplay:=rng.Text)
            Set rng = hyp.Range
            rng.Collapse wdCollapseEnd
            rng.End = ActiveDocument.Range.End
        Else
            Exit Do
        End If
    End With
Loop

End Sub

Open in new window

0
 
LVL 5

Author Closing Comment

by:SMcP
ID: 36558560
A good answer that did what it was supposd to do except for one issue I found, an infinite loop.  The answer was not actually related to the macro itself but with word itself.

I had to do a bit of debugging which has caused my workflow to change.

The problem ended up being the HL0000 references are INDEXED and the code above does not take account of this, why should it you could ask...right.

Anyway - word gets stuck on the first INDEX and never moves onto the next find entry.

So I've had to delete all my fields (60 page reference manual) do the hyperlinks and then do the Index Entreis again.

not a biggie but a gottcha that was hidden until I realised what was happening.  Always look for problems you can't see, they might be in the hidden entries.

Thanks and regards
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question