Consulting

Results 1 to 5 of 5

Thread: insert normal-style space between italics and end of paragraph marker

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Posts
    9
    Location

    insert normal-style space between italics and end of paragraph marker

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Posts
    9
    Location
    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

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Dec 2015
    Posts
    9
    Location
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •