Enter cursor dictionary type (or optional text insertion) in non-italic text after turning text to italic

When I execute the following code:

Dim italicSaveRange As Word.Range
Dim savedItalic As Variant
Dim someRange As Word.Range

Set italicSaveRange = someRange.Duplicate
italicSaveRange.Collapse (WdCollapseEnd)
savedItalic = italicSaveRange.Italic
someRange.Italic = True
italicSaveRange.Italic = savedItalic

      

I expected any text entered with the cursor or inserted in someRange

a la:

someRange.InsertAfter "Lorem ipsum..."

      

will not be italicized (assuming the formatting was not italicized at this position before, of course.) But it does. Reference.


Based on your suggestion, I now have the following which seems to work. It might be a brittle solution, depending on what is moving in that time (e.g. try typing italicized text in Word, ctrl-i to prepare for using non-italic, but then move the cursor to the left in italic and then to the right, the cursor inserts italic text ...), but for my purposes, when I add text elsewhere (but at a different level of code so that I cannot access the text to insert at that level) it will probably work, Thanks ...

Set italicSaveRange = someRange.Duplicate
italicSaveRange.Collapse (WdCollapseEnd)
savedItalic = italicSaveRange.Italic
someRange.Italic = True
italicSaveRange.InsertAfter SP
italicSaveRange.Characters(1).Italic = savedItalic
italicSaveRange.Characters(1).Delete

      

0


source to share


1 answer


When you paste text into a word range object, that text always (afaik) inherits the formatting of the previous run of the text.

In order to work, you have to apply formatting after you have inserted the text, i.e.



Dim italicSaveRange As Word.Range
Dim savedItalic As Variant
Dim someRange As Word.Range

Set italicSaveRange = someRange.Duplicate
italicSaveRange.Collapse (WdCollapseEnd)
savedItalic = italicSaveRange.Italic
someRange.Italic = True
italicSaveRange.Text = "Lorem ipsum..."
italicSaveRange.Italic = savedItalic

      

If you need to insert text later, you may need to insert some dummy text that you will replace later.

+1


source







All Articles