Consulting

Results 1 to 9 of 9

Thread: Format paragraphs that start with certain words?

  1. #1
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    5
    Location

    Format paragraphs that start with certain words?

    Hi, all,

    Using Word 2016/365, my team and I write about 50 monthly reports with headings that always start with the day, month and date, like "Monday, January 1." That's the whole paragraph.

    Anyone know how I could code VBA to, for example, take every paragraph that starts with a day of the week, turn it bold and turn on the "keep with next" feature? I could probably figure out the formatting by recording a macro formatting text, and seeing the code that it came up with. But I can't figure out how to code searching for days of the week at the start of a paragraph.

    TIA for any help!

    Phreaddy

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Simplistic, but should get the job done:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 4/24/2018
    Dim oPar As Paragraph
      For Each oPar In ActiveDocument.Range.Paragraphs
        If Trim(oPar.Range.Words(1)) Like "[MTWFS]*day" Then
          oPar.Range.Font.Bold = True
          oPar.KeepWithNext = True
        End If
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    5
    Location
    Thanks -- I'll give that a try!

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Unless you have other paragraphs with the dates at their ends, you don't actually need a macro. Simply use a wildcard Find/Replace, where:
    Find = <[MTWFS][ondayueshrit]{2,7}, [JFMASOND][anuryebchpilgstmov]{2,8} [0-9]{1,2}.^13
    Replace = ^&
    and you set the Replace character and paragraph formats with the bold and Keep With Next attributes, respectively.

    That said, I'd recommend defining a paragraph Style with the bold and Keep With Next attributes and use that as the replacement Style. You could, of course, record the above as a macro.
    Last edited by macropod; 04-25-2018 at 08:10 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Newbie
    Joined
    Mar 2017
    Posts
    5
    Location
    Thanks, folks. Gmaxey, it worked like a charm. Macropod, I'll keep a link to yours, too, in case I ever have any problems.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: http://www.msofficeforums.com/word-v...ain-words.html

    Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq...._new_faq_item3

    FWIW, Greg's macro will also process paragraphs starting with 'Today' or 'Someday', for example.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Paul,

    True, and that was why I led off with "Simplistic". But, considering his description, I thought it might work for him. We could change to::

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 4/24/2018
    Dim oPar As Paragraph
      For Each oPar In ActiveDocument.Range.Paragraphs
        If Trim(oPar.Range.Words(1)) Like "[MTWFS]*[inrs]day" Then
          oPar.Range.Font.Bold = True
          oPar.KeepWithNext = True
        End If
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Or, for something far quicker:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Text = "<[MTWFS][ondayueshrit]{2,7}, [JFMASOND][anuryebchpilgstmov]{2,8} [0-9]{1,2}.^13"
        With .Replacement
          .Text = "^&"
          .ClearFormatting
          .ParagraphFormat.KeepWithNext = True
          .Style = "Strong"
        End With
        .Format = True
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
      End With
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    An expression of a truly bright mind at work.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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