none
Preserve font when prepending a string in VBA RRS feed

  • Question

  • In Word, I have

    BOLD plain text

    where "BOLD" is bold text and the rest is plain test. Now if I run this VBA code

    Sub PrefixNm()
      sMyPar = 123 & " " & ActiveDocument.Paragraphs(1).Range.Text
      ActiveDocument.Paragraphs(1).Range.Text = sMyPar
    End Sub

    all the text will become bold. If the first word is italic, all the text will be italic. How can I prepend something without affecting the font of the existing text? (The font of the prepended number or string doesn't matter.) Where can I find description of this behavior in Microsoft documentation? Thanks.

    Tuesday, November 13, 2018 3:27 AM

Answers

  • Use:

    Sub PrefixNm()
    ActiveDocument.Paragraphs(1).Range.InsertBefore 123 & " "
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by yong321 Tuesday, November 13, 2018 4:08 PM
    Tuesday, November 13, 2018 4:20 AM
  • Try:

    Sub ParNm2()
    Application.ScreenUpdating = False
    Dim Rng As Range, p As Long
    With ActiveDocument
      For p = 1 To .Paragraphs.Count
        Set Rng = .Paragraphs(p).Range
        With Rng
          .Collapse wdCollapseStart
          .Text = p & " "
          .Font.Bold = False
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by yong321 Thursday, November 15, 2018 4:03 PM
    Thursday, November 15, 2018 1:19 AM

All replies

  • Use:

    Sub PrefixNm()
    ActiveDocument.Paragraphs(1).Range.InsertBefore 123 & " "
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by yong321 Tuesday, November 13, 2018 4:08 PM
    Tuesday, November 13, 2018 4:20 AM
  • Hi,

    Based on your description, I will move this thread to Word for Developer forum:

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=worddev

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Emi


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, November 13, 2018 8:09 AM
  • Sorry to bother you with one more question. I want to have one version of my code where the inserted text should be plain, i.e.

    BOLD plain text

    BOLD2 plain text second line

    prefixed with line numbers which should not be bold. This code

    Sub ParNm2()
     Dim rng As Range
     iParCount = ActiveDocument.Paragraphs.Count
     For J = 1 To iParCount
       Set rng = ActiveDocument.Paragraphs(J).Range
       rng.InsertBefore J & " "
       'Selection.HomeKey Unit:=wdLine
       Selection.MoveLeft Unit:=wdCharacter, Count:=1
       Selection.MoveRight Unit:=wdCharacter, Count:=Len(J), Extend:=wdExtend
       'Selection.Style = ActiveDocument.Styles("List Paragraph")
       Selection.Range.Font.Bold = False
       'rng.Font.Bold = False
     Next J
    End Sub

    always prefixes with line numbers in bold that cannot be un-bolded. The ideas of MoveLeft and Right are from https://stackoverflow.com/questions/11980728/insert-text-before-and-after-selection-and-set-style-of-new-text but tracing the execution (F8) shows that after InsertBefore, the cursor stays where it originally was in the document, without a selection that "is expanded to include the new text" as documentation says. What could be wrong? Thanks again.

    I use Microsoft Visual Basic For Applications 7.1, (C) 2012 Microsoft, if that matters.

    Wednesday, November 14, 2018 10:08 PM
  • Try:

    Sub ParNm2()
    Application.ScreenUpdating = False
    Dim Rng As Range, p As Long
    With ActiveDocument
      For p = 1 To .Paragraphs.Count
        Set Rng = .Paragraphs(p).Range
        With Rng
          .Collapse wdCollapseStart
          .Text = p & " "
          .Font.Bold = False
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by yong321 Thursday, November 15, 2018 4:03 PM
    Thursday, November 15, 2018 1:19 AM