Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 06-08-2022, 02:59 AM
Shelley Lou Shelley Lou is offline VBA Paragraph Indent under Heading styles Windows 10 VBA Paragraph Indent under Heading styles Office 2016
Expert
VBA Paragraph Indent under Heading styles
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Paragraph Indent under Heading styles


I have created a macro to indent non-numbered paragraphs under Heading 1-7 styles in legal documents. It works to a certain extent. The first part of the code indents paras using Body 1-7 styles under bold heading styles and this part works fine.

The second part of the code is where I am having issues - it is supposed to move the body level back one level but only if the previous heading style is not bold e.g. Body3 becomes Body2. I'm trying to get the code to 'do nothing' if the previous heading is bold but I can't seem to get it work correctly.

I would be really grateful if someone could take a look and tell me where I'm going wrong. Thanks

TEST DOC FOR BODY LEVELS.docx

Code:
Sub BodyLevelParas()
Application.ScreenUpdating = False
Dim para As Paragraph, nextPara As Paragraph, Rng As Range
Dim i As Integer
If Selection.Type = wdSelectionIP Then
MsgBox Prompt:="You have not selected any text!"
Exit Sub
End If
With Selection.Range
Set Rng = Selection.Range
With Rng
For Each para In .Paragraphs 'Convert non numbered paras to Body Level
            For i = 1 To 7
                If para.Style Like "Heading " & i & "*" Then
                    Set nextPara = para.Next
                    If Not nextPara Is Nothing Then
                        If nextPara.Style = "Body Text" Then
                            nextPara.Style = "Body" & i
                        End If
                    End If
                End If
            Next i
        Next para
End With
With Selection.Range
Set Rng = Selection.Range
With Rng.Find
 For Each para In Rng.Paragraphs 'Move Body Level back one level if previous heading is not bold
 If para.Range.Style = "Body2" Then
 If para.Previous.Style = "Heading 2" Then
 If para.Range.Font.Bold = True Then 'Do nothing if previous heading is bold
 Else
 para.Range.Style = "Body1"
 End If
 End If
 End If
 If para.Range.Style = "Body3" Then
 If para.Previous.Style = "Heading 3" Then
 If para.Range.Font.Bold = True Then 'Do nothing if previous heading is bold
 Else
 para.Range.Style = "Body2"
 End If
 End If
 End If
 If para.Range.Style = "Body4" Then
 If para.Previous.Style = "Heading 4" Then
 If para.Range.Font.Bold = True Then 'Do nothing if previous heading is bold
 Else
 para.Range.Style = "Body3"
 End If
 End If
 End If
 If para.Range.Style = "Body5" Then
 If para.Previous.Style = "Heading 5" Then
 If para.Range.Font.Bold = True Then 'Do nothing if previous heading is bold
 Else
 para.Range.Style = "Body4"
 End If
 End If
 End If
 If para.Range.Style = "Body6" Then
 If para.Previous.Style = "Heading 6" Then
 If para.Range.Font.Bold = True Then 'Do nothing if previous heading is bold
 Else
 para.Range.Style = "Body5"
 End If
 End If
 End If
 If para.Range.Style = "Body7" Then
 If para.Previous.Style = "Heading 7" Then
 If para.Range.Font.Bold = True Then 'Do nothing if previous heading is bold
 Else
 para.Range.Style = "Body6"
 End If
 End If
 End If
 Next para
 End With
 Application.ScreenUpdating = True
End With
End With
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Paragraph Indent under Heading styles Help Please, IF with Selected Paragraph Format & Left Indent with First Line Indent Cendrinne Word VBA 3 03-06-2022 04:42 AM
How to indent first line from second paragraph of every heading? indra059 Word 3 02-22-2022 08:20 PM
VBA Paragraph Indent under Heading styles Creating buttons for individual paragraph styles on a ribbon tab - not using Quick Styles T7Training Word 11 12-22-2019 12:16 PM
Using numbered list style, how to indent text immediately following heading to match heading indent? SpechtacularDave Word 3 09-25-2019 01:22 PM
VBA Paragraph Indent under Heading styles Styles: Heading 4 stuck at same heading number Hallet Word 1 05-31-2012 02:37 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:34 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft