PDA

View Full Version : Selection Moves Too Far



kbilling
08-29-2008, 08:50 AM
Greetings, all, from the new (old) kid on the block

I'm an experienced VB programmer, but relatively new to using the Word object model. I've encountered a problem that I've been unable to resolve and am hoping someone here has the not-so-obvious answer.

I'm enhancing a macro that finds hyperlinks in a document with missing bookmarks. The goal is to select the broken hyperlink and terminate so that the user can fix it. To do so, I collapse the Selection object to the start of the document, then move its Start to the character offset of the first character of the hyperlink. Then I move the Selection's End forward the number of characters of the range count of the hyperlink. This should select the hyperlink text, but the selection winds up much farther forward in the document. By debugging at run time, I can see the selection collapse correctly and the move methods return the correct number of characters. Below is the troublesome snippet with example character offsets.

Selection.Start = 0
Selection.Collapse wdCollapseStart
'Start goes to 59913 instead of hyperlink start of 48595
Debug.Print Selection.MoveStart(wdCharacter, hypMember.Range.Start)
'End goes to 59997 instead of hyperlink end of 48646
'hypMember.Range.End = 48682
Debug.Print Selection.MoveEnd(wdCharacter, hypMember.Range.Characters.Count)
ActiveWindow.ScrollIntoView Selection.Range, True

Any ideas what I'm doing wrong? Is it possible that ranges don't take into account things like image insertions? How are they counted, as one character?

Thanks for your help. I'll hope to pay it forward some day.

TonyJollans
08-29-2008, 12:39 PM
Don't try to outsmart Word :)

The total number of characters in the hyperlink range includes all the control information - the hyperlink itself - and the display text, BUT ...

When you move forward the end of the Selection, Word counts the number of characters actually displayed.

There are a couple of ways you can select the hyperlink but the easiest is almost certainly:
hypMember.Range.Select

kbilling
09-08-2008, 03:22 PM
Well, that's certainly easy. Thanks.