VBA Macro to justify the text of a document

I am trying to create a macro for an admin to use to justify the paragraphs for an entire document while leaving the heading of the document alone.

Here is what I have so far, which justifies the entire document, including the heading:
Sub ChangeAlign()
'
' ChangeAlign Macro
'
'
iParCount = ActiveDocument.Paragraphs.Count
For J = 1 To iParCount
    sMyPar = ActiveDocument.Paragraphs(J).Range.Text
    '[Add processing comments to manipulate sMyPar]
    Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
    ActiveDocument.Paragraphs(J).Range.Text = sMyPar
Next J


End Sub

Open in new window


I will attach an example Test Document.

The macro works, however it also justifies the heading  "SECTION 21 00 10
GENERAL REQUIREMENTS FOR FIRE PROTECTION WORK" as well as the "END OF SECTION" portion of the document.  I would like the macro to NOT affect those two sections at the beginning and end of the document.  It would have been simpler if those two items were actually part of the header and footer, but they are not, they are part of the main body of text.  I know there is a way to do this, as I had found a macro on the net before that did precisely what I'm asking, however I've lost it and the admin's PC was re-imaged so she lost the macro as well.  I can no longer locate the site I found the macro on initially so I'm starting from scratch.
Test-Document.rtf
CKilmer1975Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
You could do it on paragraph count (start on third paragraph and go to the penultimate paragraph) but this macro looks at the text.

Sub JustifyText()
    Dim para As Paragraph
    Dim rng1 As Range
    Dim rng2 As Range
    
    Set rng2 = ActiveDocument.Range
    
    Set rng1 = ActiveDocument.Range
    With rng1.Find
        .Text = "GENERAL REQUIREMENTS FOR FIRE PROTECTION WORK^p"
        If .Execute Then
            rng2.Start = rng1.End
        End If
    End With
    
    Set rng1 = ActiveDocument.Range
    With rng1.Find
        .Text = "END OF SECTION^p"
        If .Execute Then
            rng2.End = rng1.Start
        End If
    End With
    rng2.Paragraphs.Alignment = wdAlignParagraphJustify
End Sub

Open in new window

CKilmer1975Author Commented:
Thank you for your response.   Not all documents are going to be the same and I see the macro you posted include's a search for the text "GENERAL REQUIREMENTS FOR FIRE PROTECTION WORK^p".  That's just this one document.  I'm assuming that means it will only work for this one test document I attached.
GrahamSkanRetiredCommented:
If you have a more generalised set of criteria, then they could be probably be used instead, if you let us know what they are.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

CKilmer1975Author Commented:
I apologize for my ignorance in VB.  I know just enough to get myself into trouble.  Our admin has a tonne of these spec documents she has to go through and reformat to look similar with the "justification" of paragraphs.  We need the macro to justify the whole body of the document EXCEPT for the Title Heading and the 'End of Section'.  I'm not sure if that's what you mean by more generalized criteria.
GrahamSkanRetiredCommented:
Whatever the coding language, the macro must be able to distinguish between wanted text and unwanted text.

Here I have used your custom paragraph styles.

The code assumes that the alignment is to start after the first occurrence of the "Quorum Client Match Level 1N" style, which is what applies to the paragraph with text "GENERAL REQUIREMENTS FOR FIRE PROTECTION WORK" in your example.

The style for the "END OF SECTION" paragraph is "Quorum Client Match Level 0" so it is assumed the the justification ends just before that.
Sub JustifyText()
    Dim para As Paragraph
    Dim rng1 As Range
    Dim rng2 As Range
    
    Set rng2 = ActiveDocument.Range
    
    Set rng1 = ActiveDocument.Range
    With rng1.Find
        .Style = "Quorum Client Match Level 1N"
        If .Execute Then
            rng2.Start = rng1.Start
        End If
    End With
    
    rng1.End = ActiveDocument.Range.End
    With rng1.Find
        .Style = "Quorum Client Match Level 0"
        If .Execute Then
            rng2.End = rng1.Start
        End If
    End With
    rng2.Paragraphs.Alignment = wdAlignParagraphJustify
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CKilmer1975Author Commented:
You are awesome.  That did the trick!  Thank you for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.