Oscar Hermosilla
Oscar Hermosilla

Reputation: 530

I want to remove the bold / italic / underline format in the last word to continue writting normal

I am coding a program that changes the format of a list of words as you write in Ms-Word.

I am using the Range.Find.Execute to find the desired text I want to modify and then I modify the range (for example for italics)

myRange.Find.Execute(FindText:="inicialText", ReplaceWith:="newTextInItalics")
myRange.Italic = 1

Everything is working fine, except when the word I am changing is the last word. Then, the style remains in Ms-Word. So if you continue typing, the new text inserted will be in italics which I do not want to be the case.

I have also tried to collapse the range and remove italics:

myRange.Collapse(WdCollapseDirection.wdCollapseEnd)
myRange.Italic = 0

But that does not work either!

The only possible workaround I could think of is to simulate the hotkey to toggle bold (Ctrl + B) / italic (Ctrl + I) / underline (Ctrl + U) but it has many inconveniences for example it depends on the language. In Spanish Ctrl + N for bold, etc.

P.D. I am testing it with Word 2003 and 2007 but I want to be as extensive in compatibility as possible.

I am coding in VB.NET but it does not matter because if you know a way to do it in vba I will adapt the code.

Upvotes: 2

Views: 2415

Answers (1)

David J.
David J.

Reputation: 64

After you replaced the text, try to select range again and then set the italic to 0

Upvotes: 2

Related Questions