Hey Serge:
The .name top-level domain is no stranger than .biz or .gov -- Just more
exclusive
(And much less likely to attract spam...)
For all the lucky listeners that have been following along, here's the
outcome:
* * * * * *
This problem was very difficult to solve because it was being "hidden" by
coding techniques that we all do because it seems that they save time, but
they come back to bite you later when you are trying to debug.
Always include the Compiler Directive "Option Explicit" at the top of each
module, so that the compiler will warn you if you have any undeclared
variables. This is a useful check against spelling errors and type
mis-matches.
Now: A "range" includes the whole of an object, including its container. In
the case of a paragraph, the container is the paragraph mark, and the range
includes it. If you do not turn on your paragraph marks when doing
development work, you cannot see what you are doing. Stumbling around in
the dark may be exciting, but the bruised shins get old after a while
So when you anchored like this: anchor:=ActiveDocument.Paragraphs(i).Range,
you were anchoring to a range that ended immediately before the next
paragraph. When you then anchored to the next paragraph, your anchor range
began inside the previous range. So you ended up with 20 hyperlinks inside
each other.
In the code below, note that I first return the range of the paragraph I
want, and then I shrink the range so it cannot join the one following it. I
have done it two ways: the easiest way is to simply collapse the range to an
insertion point. However, if you want to include the text of the paragraph
in the hyperlink hotspot, you need to do it the second way: shrink the range
by one character to get the paragraph mark out of it.
I can understand how tempting it is to take shortcuts. But through bitter
experience, I have learned not to do it. I re-learn this lesson almost
every day... Notice how I re-named the variables to something
self-explanatory? This is so I can work out what the {naughty word}s are
doing when the code blows up!! Using short variable names just makes your
code hard to read: the compiler doesn¹t care, it reduces them all to binary
integers anyway. So use something self-explanatory, so you can see what you
are doing!!
Be careful to avoid variable names the same as keywords or VBA commands, or
you will get yourself totally confused!
Hope this helps
Option Explicit
Sub Compile_Menu1()
Dim thisPar As Range ' One Dim per line so you can see what you are doing
Dim Sel As Range ' No undefined variables, too accident-prone
Dim i As Long ' Loops control variables are longs (compiler's native binary
word) for best speed
Dim docName As String
Set Sel = Selection.Range
docName = Left(ActiveDocument.Name, 3)
'MsgBox n
' This code sets the hyperlink at the beginning of each para
'For i = 1 To Sel.Paragraphs.Count ' Initialise i here
' Set thisPar = Sel.Paragraphs(i).Range ' Choose one para
' thisPar.Collapse direction:=wdCollapseStart ' Collapse range to
insertion point
' ActiveDocument.Hyperlinks.Add _
' Anchor:=thisPar, _
' SubAddress:=docName + CStr(i)
'Next i
' If you want to include the text in the hyperlink,
' you need to contract the range instead of collapsing it
For i = 1 To Sel.Paragraphs.Count ' Initialise i here
Set thisPar = Sel.Paragraphs(i).Range ' Choose one para
thisPar.MoveEnd unit:=wdCharacter, Count:=-1 ' back one char
ActiveDocument.Hyperlinks.Add _
Anchor:=thisPar, _
SubAddress:=docName + CStr(i)
Next i
Selection.ClearFormatting ' Don't do this disgusting direct formatting
Selection.Font.Name = "Arial" ' Define a style and use it!!
End Sub
--
Don't wait for your answer, click here:
http://www.word.mvps.org/
Please reply in the group. Please do NOT email me unless I ask you to.
John McGhie, Microsoft MVP, Word and Word:Mac
Sydney, Australia. mailto:
[email protected]