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
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