Consulting

Results 1 to 4 of 4

Thread: Formatting bullets in Word

  1. #1

    Formatting bullets in Word

    I have a Word document with a large number of bulletted lists with Normal text between the lists. I have code that formats all bullets in the same way (line spacing etc,) . What I want to do is look at the last character of each bullet and determine if it is a ";" but also have a "." at the end of the last bullet in a list. The intent is to have all the bullets formatted in the same way, that is, semi-colons at the end of each bullet and the last bullet has a full stop instead of a semi-colon. e.g. the red text below is the action I want the macro to do for each bullet.


    • Sample Text 1 - this needs a ";"
    • Sample Text 2; - do nothing
    • Sample Text 3 - this needs a "."


    Then some additional paragraphs.


    • Sample Text 4; - do nothing
    • Sample Text 5 - this needs a ";"
    • Sample Text 6. - do nothing


    Ive tried a few things but with no success. Any suggestion? Thanks.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You really should define a suitable Style for your Bullet paragraphs instead of overriding Word's Style definitions for the underlying paragraphs. You then wouldn't need a macro for the formatting; you'd do it all via the Style definition. That is how Word is supposed to be used.

    Control of the last character in each list could then be done via a Find/Replace macro that looks for instances of that Style. For example:
    Sub Demo()
    Application.ScreenUpdating = False
    Const StrSty As String = "Bullet"
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^p"
        .Style = StrSty
        .Replacement.Text = ""
        .Forward = True
        .Format = True
        .Wrap = wdFindStop
        .MatchWildcards = False
        .Execute
      End With
      Do While .Find.Found
        If .End = ActiveDocument.Range.End Then
          If .Characters.First.Previous Like "[,.;:]" Then
            .Characters.First.Previous = "."
          Else
            .InsertBefore "."
          End If
          Exit Do
        End If
        If .Paragraphs.First.Next.Style = StrSty Then
          If .Characters.First.Previous Like "[,.;:]" Then
            .Characters.First.Previous = ";"
          Else
            .InsertBefore ";"
          End If
        Else
          If .Characters.First.Previous Like "[,.;:]" Then
            .Characters.First.Previous = "."
          Else
            .InsertBefore "."
          End If
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    where 'Bullet' is your Bullet Style's name.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3

    Formatting Bullets in Word

    Thanks, I was hoping for a more generic approach so others can use it with the default bullets rather than having to use a bespoke style.

    Is it possible to look at a paragraph to determine if its "WdListType.wdListBullet" and the next paragraph to determine if its also a "WdListType.wdListBullet". If this is the case use RIGHT(bullettext,1) to determine if it is a ";" and, if not, use bullettext= bullettext & ";" If the next paragraph is not a "WdListType.wdListBullet", use the same approach to add a "." if the last character is not a "." Hope that makes sense!

    Thanks
    Last edited by macropod; 10-17-2017 at 11:56 PM. Reason: Deleted unnecessary quote of entire post replied to

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by noclass1980 View Post
    I was hoping for a more generic approach so others can use it with the default bullets rather than having to use a bespoke style.
    As I said before, you should learn to use Styles properly; they are the foundation of any properly-structured Word document. Without that, the best you can do is look for the 'List Paragraph' Style (which is what you get if you apply bullets without applying a predefined Style) and add whatever additional code is needed for the macro I supplied to differentiate between such paragraphs that use bullets and those that don't. I'm not inclined to write code to compensate for poor document design, thereby encouraging its continuance. Your refusal to use Styles properly already has you unnecessarily and inefficiently formatting bullets via a macro...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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