Selection/Range different in Word 2010 ???

G

Gary Hillerson

I'm testing my code on Office 2010 (latest Beta). Most everything
works, except i'm hitting a strange problem in one of my functions
that inserts text (piece by piece) into a doc.

I'm calling this function from another function that takes a string
with markup in it, e.g.

"Name, G. (2002). <i>Title of paper</i>. Santa Cruz, CA."

and turning it into a paragraph. I "walk" through the string, breaking
it into pieces that have or don't have markup. And then I insert each
piece at the end of a paragraph; if the piece has markup, then the
function tells Word to apply its font formatting (e.g. make the text
italic if the original string markup was <i>...</i>)

This function works fine in Word 2007, Word 2003, Word 2000, and Word
97. It's broken in Word 2010. In debugging, I know where it's broken,
but i'm not sure how to fix it.

The code for the function is below. Where it is broken is in the
statement:
Set rng = Selection.Range

In Word 2007, after that statement, rng.Text is the value of the
"theText" parameter that I've just added to the end of the selection.

In Word 2010, after setting the rng, rng.Text is empty.

I'm guessing I've been doing something wrong all along and getting
away with it in past versions, but i don't know what. Any help
appreciated.

-------------------------------------------------------------------------------
Public Sub InsertTextAfterSelection(ByVal theText As String, _
ByVal theStyle As String, _
ByVal theAttr As Integer)
Dim rng As Range

Selection.Collapse direction:=wdCollapseEnd
Selection.InsertAfter theText
If theStyle <> "" Then
Selection.Style = theStyle
End If
Set rng = Selection.Range
rng.Italic = False
rng.Bold = False
rng.Underline = False
rng.Font.Superscript = False
Select Case theAttr
Case eTextAttrItalic
rng.Italic = True
Case eTextAttrBold
rng.Bold = True
Case eTextAttrUnderline
rng.Underline = True
Case eTextAttrSuperScript
rng.Font.Superscript = True
Case Else
'do nothing
End Select
End Sub
 
J

Jay Freedman

Hi Gary,

I'm not aware of any difference in this area between 2010 and previous
versions. Right now I'm not at the computer that has 2010 so I can't test
until later (and I have the final, RTM version from MSDN rather than the
beta). But I'll suggest that, for all versions, you should replace this
section of your macro

Selection.Collapse direction:=wdCollapseEnd
Selection.InsertAfter theText
If theStyle <> "" Then
Selection.Style = theStyle
End If
Set rng = Selection.Range

with this...

Set rng = Selection.Range
rng.Collapse direction:=wdCollapseEnd
rng.Text = theText
If theStyle <> "" Then
rng.Style = theStyle
End If

and move the Selection.Collapse direction:=wdCollapseEnd statement to the
end, just before End Sub.

The statement rng.Text = theText guarantees that rng will cover the inserted
text, and everything after that should just work.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Gary Hillerson

Hi Jay,

Thanks for your help.

Unfortunately, that didn't change anything, but I now see what it is
doing, though I still don't see why. The result that's getting
generated has my "chunks" in reverse order, e.g. when I process this
marked up string:
Gary Hillerson. (2002). <i>Title of my work</i>. NY: Guilford.

While debugging i see my function get called three times, as it
should:
1) theText = "Gary Hillerson. (2002). ", attr=0
2) theText = "Title of my work. " attr = 1
3) theText = "NY: Guilford."

And the result I get is
NY: Guilford. Title of my work. Gary Hillerson. (2002).

And the title is not italicized.

Clearly something fundamental is going wrong, as if wdCollapseEnd is
not working correctly. I've gotta go to a meeting for a few hours,
will be back at it later. If you have any further insights, let me
know.

And thanks Again

- gary
 
J

Jay Freedman

Sorry, that was my mistake. At the end of the sub, instead of just
collapsing the Selection (which hasn't been moved from where it was
before the sub started), put this:

rng.Select
Selection.Collapse direction:=wdCollapseEnd

That will set things up for the next call.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Gary Hillerson

Thanks, Jay.

That worked, though i had to move my Style assignment elsewhere -- the
interaction between setting the attribute of the range and the style
of the paragraph got things confused, and it really belonged
elsewhere, since it applied to the entire paragraph and shouldn't have
been assigned to each chunk.

Your diligence and knowledge are appreciated!


g
 

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