Solved

Word VBA Hyperlinks

Posted on 2011-09-18
2
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

719 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