Range messes up after adding hyperlink

J

jdonkers

I am trying to move through some text and change some to hyperlinks as below
--Before (all text)---------------------
Test1-----Move to T2
Test2-----Move to T1
----------------------------------------
After-->The "Move to T2" and "Move to T1" will be hyperlinks to Test2 and
Test1 respectively.

If I put a watch on aRange I see the following as written in the comments
for the code I am using.

Dim aRange as Word.Range
Set aRange = ActiveDocument.Range(Start:=10, End:=20)
' Watch says "Move to T2" as expected, and aRange.End is 20.

ActiveDocument.Hyperlinks.Add Anchor:=aRange,
Address:=ActiveDocument.FullName, SubAddress:="Test2"
' Watch still says "Move to T2" as expected, but aRange.End changed from 20
to 123.

Set aRange = ActiveDocument.Range(Start:=30, End:=40)
' Watch says "uments and" instead of "Move to T1" as expected. This looks
like part of the path name

Could someone please explain what's going on and what I am doing wrong?
 
W

Word Heretic

G'day "jdonkers" <[email protected]>,

When you add that hyperlink, the hyperlink field itself requires room
in the document, thus your .end moves. Then, for some strange reason
you are starting your second range at 30 instead of 10 as per first
time.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


jdonkers reckoned:
 
J

jdonkers

Actually, that last solution didn't work. This one does and looks *much*
nicer!

Function CreateHyperlinks()
Dim colLabels As New Collection
' Move through document and create links at each perform statement that
will go to that code block.
' The bookmark can't start with a digit or contain a hyphen, so we will
have to format the names as we go.
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True

Dim myRange As Range
Dim thisLabel As String
Dim blnDone
blnDone = False

With ActiveDocument
Set myRange = .Content
While (blnDone <> True)
' Determine the label name
myRange.Find.Execute FindText:="PERFORM", Forward:=True,
MatchCase:=False
If myRange.Find.Found = True Then
' Determine the label by moving though until the first
alphanum is hit and then move to whitespace
myRange.Collapse Direction:=wdCollapseEnd
myRange.MoveEndUntil "abcdefghijklmnopqrstuvwxyz0123456789"
labelStart = myRange.End
myRange.Collapse Direction:=wdCollapseEnd
myRange.MoveEndUntil " " + vbCr + vbCrLf + vbTab
labelEnd = myRange.End
thisLabel = myRange.Text ' Mid(docText, labelStart, labelEnd
- posEnd)
If (thisLabel <> "VARYING") Then
formattedLabel = FormatLabel(thisLabel)
.Content.Hyperlinks.Add Anchor:=myRange,
Address:=.FullName, SubAddress:=formattedLabel, TextToDisplay:=thisLabel

' Add this label to the collection
colLabels.Add thisLabel
myRange.SetRange myRange.End, .Content.End
End If
Else
blnDone = True
End If
Wend
End With
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
Set CreateHyperlinks = colLabels
End Function
 
W

Word Heretic

G'day "jdonkers" <[email protected]>,

Much better. Do change your

If myRange.Find.Found = True

to

If myRange.Find.Found

as tis blasphemous.

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


jdonkers reckoned:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top