Carl G
Carl G

Reputation: 18260

Make Word's Cursor Type (Or More Text Insert) in Non-Italic After Turning Text Italic

When I perform 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 that any text entered at the cursor or inserted into someRange a la:

someRange.InsertAfter "Lorem ipsum..."

would not be italic (assuming that the formatting was not italic at this position previously, of course.) But it is. Help.


Based on your suggestion I now have the following, which appears to be working. It may be a fragile solution, depending on what gets moved around in the meantime (e.g., try typing some italic text into Word, ctrl-i to prepare to use non-italic, but then move the cursor left into the italics and then right out again; the cursor is inserting italic text...), but for my purposes where I am adding text elsewhere (but at a different level of the code so that I cannot access the text to insert at this level) this will probably work. Thank you.

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

Upvotes: 0

Views: 1336

Answers (1)

Dirk Vollmar
Dirk Vollmar

Reputation: 176169

When you insert text into a Word Range object, this text will always (afaik) inherit the formatting of the previous text run.

To work around you should apply the formatting after you 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 must insert the text later you could need to insert some dummy text which you replace later.

Upvotes: 1

Related Questions