PDA

View Full Version : [SOLVED:] insert normal-style space between italics and end of paragraph marker



kkkwj
08-09-2018, 10:08 AM
I am trying to write a macro to insert a single space with Normal style after the insertion point. The problem is that the insertion point is between an italics character on the left and the paragraph marker on the right. I could be wrong, but it seems to me that the paragraph marker carries some style information with it. Here is my code – I have already spent an hour on the problem without success.

My algorithm is to insert a space before the insertion point (on the left of the selection bar), then extend the selection backwards one character, assign the normal style to that selection range, and collapse the selection, leaving the insertion point between a normal space on the left and the paragraph marker on the right.


I must be doing something wrong. Does anyone know what it is? Thank you




Sub FNormal()
' insert a normal space after an italic word adjacent to the end paragraph marker
selection.Collapse wdCollapseEnd
selection.InsertBefore " "
selection.MoveLeft Unit:=wdCharacter, count:=1, Extend:=wdExtend
selection.style = ActiveDocument.Styles("Normal")
selection.Collapse wdCollapseEnd
End Sub

macropod
08-09-2018, 03:24 PM
There is no 'Normal' character Style; 'Normal' is a paragraph Style, so you can't apply it to a single character in a paragraph. Presumably you want something like:

Sub Demo()
With Selection
.Collapse wdCollapseEnd
.Text = " "
.Font.Reset
End With
End Sub
or:

Sub Demo()
With Selection
.Collapse wdCollapseEnd
.Text = " "
.Font.Italic = False
End With
End Sub

kkkwj
08-09-2018, 08:16 PM
Gosh, you make it look so easy. And now that you reminded me that Normal is a paragraph style and Italics is font, I see your point exactly. Does that mean that selection.Style=X is only responsive to character styles? Or maybe assigning Normal to the selection.style makes the assignment but the assigned style won't override the font italics setting?

And for learning purposes (trying to understand the places where I went wrong), is there a difference between selection.InsertBefore and selection.text = "whatever"? In both cases wouldn't the "whatever" get inserted before the insertion point? Thank you

macropod
08-09-2018, 09:43 PM
The effect of Selection.Style=X really depends on what kind of Style you're applying and, if it's a paragraph Style, how much of a paragraph is selected. In any event, it's most unlikely to do anything when only one character is selected - and it wouldn't change italicised text back to non-italics.

You could use .InsertBefore (or even .InsertAfter) instead of .Text = and get the same results - in all cases, the inserted space is what ends up being selected.

kkkwj
08-11-2018, 09:00 AM
As always, thank you Paul! You're the master! You already know this, but I'd like to say it again: Thank you! Programmers can easily waste hours trying to find the magic syntax or magic moves, even though they're only 1 line of code away from a solution. I'm sure you have saved people thousands of hours of time over the years. Thank you again!