Prev: Missing red squigglies when automating WORD.
Next: How to make scanned document readable when opened in word?
From: Gary Hillerson on 4 Jun 2010 15:57 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
From: Jay Freedman on 4 Jun 2010 16:59 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 FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. Gary Hillerson wrote: > 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
From: Gary Hillerson on 4 Jun 2010 17:53 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 On Fri, 4 Jun 2010 16:59:18 -0400, "Jay Freedman" <jay.freedman(a)verizon.net> wrote: > >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.
From: "Tony Jollans" My forename at my surname dot on 4 Jun 2010 20:07 I get exactly the same in 2007 and 2010 (RTM). -- Enjoy, Tony www.WordArticles.com "Gary Hillerson" <garyh(a)hillysun.net> wrote in message news:5vli06h247dschpd6u5s3k46jgq60587ps(a)4ax.com... > 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
From: Jay Freedman on 4 Jun 2010 22:29 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 FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. On Fri, 04 Jun 2010 14:53:18 -0700, Gary Hillerson <garyh(a)hillysun.net> wrote: >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 > >On Fri, 4 Jun 2010 16:59:18 -0400, "Jay Freedman" ><jay.freedman(a)verizon.net> wrote: > >> >>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.
|
Next
|
Last
Pages: 1 2 Prev: Missing red squigglies when automating WORD. Next: How to make scanned document readable when opened in word? |