Consulting

Results 1 to 3 of 3

Thread: formatting lost

  1. #1

    formatting lost

    I have a Word document with the following content:
    1 2 3
    Number 2 is bold.
    I want to insert a text (Hello!) immediately after the number 1.
    The VBA program below performs the insertion:

    Sub forum()
    Set X = Selection.Range
    Set Y = X.Paragraphs(1)
    Set Z = Y.Range
    Z.Select
    Selection.CopyFormat
    X.Text = Left(Z, 1) & "Hello!" & Mid(Z, 2)
    Z.Select
    Selection.PasteFormat
    End Sub

    The result is:
    1Hello! 2 3

    Formatting is lost (no bold).
    How can I keep the same effect but keep the formatting?

  2. #2
    X.Text = removes the bold. What you want is something like
    Sub forum()Dim x As Range
        Set x = Selection.Paragraphs(1).Range
        Do Until x.Characters(1).Font.Bold = True
            x.Start = x.Start + 1
        Loop
        x.Collapse 1
        x.Text = "Hello! "
        x.Font.Bold = True
        Set x = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thank you.

Posting Permissions

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