3
votes

Writing a macro to automatically fix paraphrase spacing issues in MS Word docs generated from software we use.

Goal:

  • All standard paragraphs have 0pt before and after spacing.
  • All bullet lists have 3pt before and after spacing.

Progress:
Currently I have a function that sets the entire document to 0pt, then looks through for all lists and changes them to 3pt. (currently also have a highlight on so I can easily see what is being treated as a list).

It works great on some parts, but on other parts (I assume based on how the software we use generates the document), the list doesn't exist and it will continue to format blocks of text and heading to 3pt when it is not wanted (see attached images).

Current code is:

Sub Paragraph()
    ActiveDocument.Range.ParagraphFormat.SpaceAfter = 0
    ActiveDocument.Range.ParagraphFormat.SpaceBefore = 0

    Dim li As Word.list

    For Each li In ActiveDocument.lists
            li.Range.ParagraphFormat.SpaceBefore = 3
            li.Range.ParagraphFormat.SpaceAfter = 3
            li.Range.HighlightColorIndex = wdYellow
    Next li
End Sub

Working:

Working

Not working:

Not working

2
Does the issue usually happen when the list is just one item? - Joseph
No, this does not seem to impact it. - Shane Harris
How many documents are we talking about? - ashleedawg
...also, I'm surprised no one else asked: what software is generating these documents? (Why not fix the source of the problem?) - ashleedawg
Hi ashleedawg, This is on one document at a time. Getting fixed from the software end has been an ongoing battle long before my time and is just not going to happen. We have been told of other users who apply their own macros to solve the problems and am hoping we can do the same to greatly improve productivity. - Shane Harris

2 Answers

2
votes

According to the MSDN:

List Object: Represents a single list format that's been applied to specified paragraphs in a document.

So if you have more than one list with some non-bulleted paragraph(s) in the middle, the Range will start with the first item of the first list and end with the last item of the last list including all non-bulleted paragraph(s) in the middle.

To fix this issue, you need to separate the lists (right-click on the bullet and select Separate List). However, you mentioned that the document was generated by some software, so that is probably not an option. In that case, you will have to iterate though the paragraphs of the Range of each List and check if it has a ListFormat.ListTemplate which indicates that it is a list item, otherwise it is a non-bulleted paragraph:

Sub Paragraph()
    ActiveDocument.Range.ParagraphFormat.SpaceAfter = 0
    ActiveDocument.Range.ParagraphFormat.SpaceBefore = 0

    Dim li As Word.List
    Dim p As Paragraph

    For Each li In ActiveDocument.Lists
        For Each p In li.Range.Paragraphs
            If Not p.Range.ListFormat.ListTemplate Is Nothing Then
                p.Range.ParagraphFormat.SpaceBefore = 3
                p.Range.ParagraphFormat.SpaceAfter = 3
                p.Range.HighlightColorIndex = wdYellow
            End If
        Next p
    Next li
End Sub
0
votes

Even before touching VBA:

  • Use Styles in the document.
  • Limit the use of Styles in the document to only those that are in the template.
  • Set your spacing in the Styles.

If, at some stage, you change your mind and want to use 6pt spacing, you can adjust the template and re-apply it, rather than finding all the VBA code and re-writing it. Not only that, but by using Styles, you can avoid having VBA code, or having VBA-enabled documents which may interfere with some corporate security settings.

Oh, and set up your corporate structure to limit the use of templates to only the approved ones.