Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Word VBA Hyperlinks

Posted on 2011-09-18
2
Medium Priority
?
415 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 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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:
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

916 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