#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
Try this way of doing it. I added an inputbox to deal with a situation where the first selected paragraph isn't a heading.
Code:
Sub ApplyNormNumStyles() Dim aPara As Paragraph, iLev As Integer, iStart As Integer, sStyName As String Dim bBold As Boolean, aRng As Range Set aRng = Selection.Range If Selection.Type = wdSelectionIP Then MsgBox Prompt:="You have not selected any text!" Exit Sub ElseIf aRng.Paragraphs(1).OutlineLevel = wdOutlineLevelBodyText Then iLev = InputBox("What level heading precedes your selected text?", "Starts at Level", "1") End If For Each aPara In aRng.Paragraphs sStyName = Split(aPara.Style, ",")(0) 'removes aliases from stylename aPara.Range.Select Select Case True Case sStyName Like "Heading *" 'keep track of the preceding heading level iLev = aPara.OutlineLevel bBold = aPara.Range.Words(1).Font.Bold Case sStyName Like "Body*", sStyName Like "Normal" If iLev < 8 And aPara.Range.Characters.Count > 1 Then If bBold Then aPara.Style = "Body" & iLev Else aPara.Style = "Body" & iLev - 1 End If End If End Select Next aPara End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
VBA Paragraph Indent under Heading styles
Hi Andrew, thank you so much for taking the time to look at the code for me, I have tested your code on a few legal documents this morning and it is brilliant.
I thought I would be able to recreate the same code for Schedule Level numbering / Body styles. I changed Heading to Schedule Level in your code but it didn't work. Do I need to add something to OutlineLevel perhaps? Code:
Sub BodyLevel_Schedule() Dim aPara As Paragraph, iLev As Integer, iStart As Integer, sStyName As String Dim bBold As Boolean, aRng As Range Set aRng = Selection.Range If Selection.Type = wdSelectionIP Then MsgBox Prompt:="You have not selected any text!" Exit Sub ElseIf aRng.Paragraphs(1).OutlineLevel = wdOutlineLevelBodyText Then iLev = InputBox("What level heading precedes your selected text?", "Starts at Level", "1") End If For Each aPara In aRng.Paragraphs sStyName = Split(aPara.Style, ",")(0) 'removes aliases from stylename aPara.Range.Select Select Case True Case sStyName Like "Schedule Level *" 'keep track of the preceding Schedule Level iLev = aPara.OutlineLevel bBold = aPara.Range.Words(1).Font.Bold Case sStyName Like "Body*", sStyName Like "Normal" If iLev < 8 And aPara.Range.Characters.Count > 1 Then If bBold Then aPara.Style = "Body" & iLev Else aPara.Style = "Body" & iLev - 1 End If End If End Select Next aPara End Sub |
#4
|
||||
|
||||
Outline levels are a paragraph setting which Word locks for the built-in "Heading #" styles. You can also assign them to other styles as well. The 'Schedule Level #' styles are going to be custom styles which could have an outline level assigned to them. If you did that before running the macro it would work in the same way.
There are other ways to keep track of the number level of the preceding heading. I just used OutlineLevel because your original sample used the built-in Headings. You could also harvest the value that comes where you have put * in "Schedule Level *" eg iLev = CInt(Split(sStyName," ")(2))
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
VBA Paragraph Indent under Heading styles
Thank you so much Andrew that line of code worked perfectly for the schedule levels
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
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 |
Styles: Heading 4 stuck at same heading number | Hallet | Word | 1 | 05-31-2012 02:37 PM |