none
Automatic formatting of first word in paragraph RRS feed

  • Question

  • I use this macro to auto format the first word in all paragraphs, it opens the font dialog and then applies the chosen formatting to all first words in all paragraphs.

    I'm experiencing some trouble with it, in some cases (it seems to be random) it won't apply the formatting to some paragraphs, and sometimes it will apply the formatting to random words in middle of a paragraph.

    Any help will be appreciated.

    Sub Format_First_Word()
        Set dlgfont = Dialogs(wdDialogFormatFont)
        dlgfont.Show
        For Each para In ActiveDocument.Paragraphs
            If para.Alignment = wdAlignParagraphJustify Then
                Selection.Extend " "
                If InStr(Selection, ")") Or InStr(Selection, "]") Then Selection.Extend " "
                With Selection
                    .Font.NameBi = dlgfont.Fontnamebi: .Font.BoldBi = dlgfont.BoldBi: .Font.SizeBi = dlgfont.pointsbi
                End With
            End If
            Selection.MoveDown Unit:=wdParagraph
        Next
    End Sub
    


    Thursday, February 16, 2012 1:26 PM

Answers

  • Hi Moishy,

    Your macro is designed to work only with justified paragraphs and to apply the formatting to one word ordinarily, or two words if the first 'word' contains ')' or ']'. However, if the insertion point isn't at the start of the document, suprious results will be returned.

    Try:
    Sub Format_First_Word()
    Application.ScreenUpdating = False
    Dim dlgfont, para As Paragraph, Rng As Range
    Set dlgfont = Dialogs(wdDialogFormatFont)
    dlgfont.Show
    For Each para In ActiveDocument.Paragraphs
      If para.Alignment = wdAlignParagraphJustify Then
        Set Rng = para.Range.Characters.First
        With Rng
          .MoveEndUntil " "
          If InStr(.Text, ")") Or InStr(.Text, "]") Then
            .MoveEnd wdWord, 1
            .MoveEndUntil " "
          End If
          .Font.NameBi = dlgfont.Fontnamebi: .Font.BoldBi = dlgfont.BoldBi: .Font.SizeBi = dlgfont.pointsbi
        End With
      End If
    Next
    Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by moishy Sunday, February 19, 2012 8:10 AM
    Saturday, February 18, 2012 8:16 AM
  • Hi Moishy,

    I'd have thought your own version of the macro would have formatted anything that was already selected too. To resolve that, try adding:
    ActiveDocument.Undo
    After:
    dlgfont.Show


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by moishy Sunday, February 19, 2012 8:10 AM
    Sunday, February 19, 2012 5:38 AM

All replies

  • Hi Moishy

    I don't understand what you're doing with all the Selection.Extend calls. You don't comment the code, so it's a mystery...

    However, if you want the first word in every paragraph, then try something like this, addressing the Word collection:

    para.Range.Words(1).Font.Name = "Arial"


    Cindy Meister, VSTO/Word MVP

    Thursday, February 16, 2012 1:39 PM
    Moderator
  • Cindy,

    Thank you for giving time to my question.

    Unfourtunetly I don't really understand the macro myself (found it awhile back someware in the net, but I can't remember where).

    My guess would be that the macro should only apply changes to paragraphs where Alignment = wdAlignParagraphJustify.
    • Edited by moishy Friday, February 17, 2012 6:00 AM
    Friday, February 17, 2012 1:52 AM
  • Hi Moishy,

    Your macro is designed to work only with justified paragraphs and to apply the formatting to one word ordinarily, or two words if the first 'word' contains ')' or ']'. However, if the insertion point isn't at the start of the document, suprious results will be returned.

    Try:
    Sub Format_First_Word()
    Application.ScreenUpdating = False
    Dim dlgfont, para As Paragraph, Rng As Range
    Set dlgfont = Dialogs(wdDialogFormatFont)
    dlgfont.Show
    For Each para In ActiveDocument.Paragraphs
      If para.Alignment = wdAlignParagraphJustify Then
        Set Rng = para.Range.Characters.First
        With Rng
          .MoveEndUntil " "
          If InStr(.Text, ")") Or InStr(.Text, "]") Then
            .MoveEnd wdWord, 1
            .MoveEndUntil " "
          End If
          .Font.NameBi = dlgfont.Fontnamebi: .Font.BoldBi = dlgfont.BoldBi: .Font.SizeBi = dlgfont.pointsbi
        End With
      End If
    Next
    Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by moishy Sunday, February 19, 2012 8:10 AM
    Saturday, February 18, 2012 8:16 AM
  • Paul,

    Thank you seems to work well. Although one issue remains, if there is selected text in the document it will apply the formatting to all first words in the paragraph except the paragraph with the selected text where the selected text gets formatted instead of the first word in the paragraph.

    Saturday, February 18, 2012 11:30 PM
  • Hi Moishy,

    I'd have thought your own version of the macro would have formatted anything that was already selected too. To resolve that, try adding:
    ActiveDocument.Undo
    After:
    dlgfont.Show


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by moishy Sunday, February 19, 2012 8:10 AM
    Sunday, February 19, 2012 5:38 AM