#1
|
|||
|
|||
VBA Help Indent Paragraphs between Headings
Test Doc to replace non numbered paras under numbered paras.docx
Hi, I'm hoping someone can help me with a macro I am trying to create that indents non-numbered paragraphs under Heading 1-7 but ignores if the next paragraph is a heading number, so: Heading 1 = Body1 Heading 2 = Body2 Heading 3 = Body3 Heading 4 = Body4 Heading 5 = Body5 Heading 6 = Body6 Heading 7 = Body7 So far the code works for Heading 1 but is removing the heading 2 numbering and making those as Body1. I haven't got as far as adding in the other heading/body levels until I can sort out this problem first. If anyone can advise what I'm missing in the code I would be really grateful. Thanks, Shelley Code:
Sub ChangeParasAfterHeading() Dim para As Paragraph, nextPara As Paragraph, oRng As Range If Selection.Type = wdSelectionIP Then MsgBox Prompt:="You have not selected any text!" Exit Sub End If Set oRng = Selection.Range With oRng For Each para In oRng.Paragraphs If para.Style = "Heading 1" Then Set nextPara = para.Next If Not nextPara Is Nothing Then nextPara.Style = "Body1" Else If para.Style = "Heading 2" Then Set nextPara = para.Next If Not nextPara Is Nothing Then nextPara.Style = "Body2" Else End If End If End If End If Next End With End Sub |
#2
|
||||
|
||||
Without the document it is difficult to test, but ...
Code:
Sub ChangeParasAfterHeading() Dim para As Paragraph, nextPara As Paragraph Dim oRng As Range If Selection.Type = wdSelectionIP Then MsgBox Prompt:="You have not selected any text!" Exit Sub End If Set oRng = Selection.Range With oRng For Each para In oRng.Paragraphs Select Case para.Style Case Is = "Heading 1" Set nextPara = para.Next If Not nextPara Is Nothing Then nextPara.Style = "Body1" End If Case Is = "Heading 2" Set nextPara = para.Next If Not nextPara Is Nothing Then nextPara.Style = "Body2" End If Case Else End Select Next para End With End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
VBA Help Indent Paragraphs between Headings
Hi Graham, thank you for replying. I attached a test document in Post 1 if that helps. I have run your code but unfortunately it still removes the Heading 2 numbering and replaces it with Body1.
|
#4
|
||||
|
||||
Based on your example document:
Code:
Sub ChangeParasAfterHeading() Dim para As Paragraph, nextPara As Paragraph Dim oRng As Range Dim i As Integer If Selection.Type = wdSelectionIP Then MsgBox Prompt:="You have not selected any text!" Exit Sub End If Set oRng = Selection.Range With oRng For Each para In oRng.Paragraphs 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 lbl_Exit: Set oPara = Nothing Set nextPara = Nothing Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
VBA Help Indent Paragraphs between Headings
Hi Graham, thank you so much for the updated code, it really is appreciated and has worked very well. I have now added this to my manual to auto numbering macro and have just realised a couple of things I hadn't taken into consideration with the indents now the full code has run.
Once the auto numbering has inserted, if there is only one numbered paragraph e.g. 1, 1.1, 2, how can I tell the code to remove the auto number 1.1 and replace (in this example) with Body 1. If there are 2 or more previous numbered paragraphs e.g. 2.1, 2.2 and the following paragraph is not numbered, how can I tell the code that the non-numbered paragraph (in this example) should be Body 1 as these types of indents should sit directly under the previous number. I have attached a Word document to show the indents. TEST DOC FOR BODY LEVELS.docx Body Level.PNG |
#6
|
|||
|
|||
Hello Shelley,
On the back end, you will want to update your Style Set and your Heading Styles. It looks like there are many duplicate Styles defined and for the Heading Styles in use the "Style for the following Paragraph" should be set to the matching Body style. |
#7
|
|||
|
|||
VBA Help Indent Paragraphs between Headings
I have been working on updating the code over the weekend and have now got the code to update the body levels to be directly under the previous clause number if there are 2 or more clauses above the non-numbered text.
I just need to fathom out how to remove the numbering if there is only one paragraph between two headings now which I'm sure I'll get there in the end, I will keep researching until I find the answer. Massive thank you to this forum and Graham in particular for helping me get this far with the code, couldn't have done it without you. Code:
Sub TestChangeParas() Dim para As Paragraph, nextPara As Paragraph Dim oRng As Range Dim i As Integer If Selection.Type = wdSelectionIP Then MsgBox Prompt:="You have not selected any text!" Exit Sub End If Set oRng = Selection.Range With oRng For Each para In oRng.Paragraphs 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 Set oRng = Selection.Range With oRng.Find .Font.Bold = False For Each para In oRng.Paragraphs If para.Range.Style = "Body2" Then If para.Previous.Style = "Heading 2" Then para.Range.Style = "Body1" End If End If If para.Range.Style = "Body3" Then If para.Previous.Style = "Heading 3" Then para.Range.Style = "Body2" End If End If If para.Range.Style = "Body4" Then If para.Previous.Style = "Heading 4" Then para.Range.Style = "Body3" End If End If If para.Range.Style = "Body5" Then If para.Previous.Style = "Heading 5" Then para.Range.Style = "Body4" End If End If If para.Range.Style = "Body6" Then If para.Previous.Style = "Heading 6" Then para.Range.Style = "Body5" End If End If If para.Range.Style = "Body7" Then If para.Previous.Style = "Heading 7" Then para.Range.Style = "Body6" End If End If Next lbl_Exit: Set para = Nothing Set nextPara = Nothing Set oRng = Nothing Exit Sub End With End Sub |
#8
|
||||
|
||||
I have given myself a headache trying to work out the various combinations without much success.
The following function will give you the outline level number for a selected paragraph, which should help work out what the previous and following paragraphs are: Code:
Private Function ListParaNum(oPara As Paragraph) As String Dim i As Integer Dim xRefs As Variant xRefs = ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem) ListParaNum = 0 With oPara.Range If .ListParagraphs.Count = 0 Then GoTo lbl_Exit End If .Collapse wdCollapseEnd On Error Resume Next For i = 1 To UBound(xRefs) If ActiveDocument.ListParagraphs(i).Range.End = .End Then ListParaNum = Val(ActiveDocument.ListParagraphs(i).Range.ListFormat.ListString) Exit For End If Next i End With lbl_Exit: Exit Function End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#9
|
|||
|
|||
VBA Help Indent Paragraphs between Headings
Hi Graham, I am slowly but surely putting together a complete manual to auto (and body) numbering macro or macros to run on from each other, to make house styling documents quicker, as currently I seem to be doing the same updating receptively day in day out and just know there are quicker ways to complete these types of tasks.
Thank you for your code but I'm unsure where I should put this in the code. I copied it over under the code in the post above but when I clicked Run, it opened up a separate macro window which I wasn't sure about. I'm probably doing it totally incorrect of course, should I have added a Sub and End Sub? |
#10
|
||||
|
||||
The code itself goes in the same module as the rest of your code.
The code is a function to be called from your main process. e.g. after say NextPara has been declared you can use it to check what the paragraph level number is. To test it put the cursor in a paragraph and run the following macro from the same module as the function Code:
Sub Test() Dim para As Paragraph Set para = Selection.Paragraphs(1) MsgBox ListParaNum(para) End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#11
|
|||
|
|||
VBA Help Indent Paragraphs between Headings
Fabulous thanks so much Graham for your help, best Shelley
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Selectively numbered paragraphs based on headings.. | WildfireS3 | Word | 2 | 05-18-2020 05:18 AM |
Indent on headings | nerion | Word | 5 | 04-11-2019 04:48 AM |
Difference between first line Indent and Left Indent | kingston123 | Word | 3 | 09-25-2018 02:47 PM |
Indent of first numbered heading different from subsequent headings | ultimateguy | Word | 1 | 08-12-2015 06:51 AM |
Problem with bold numbers in multi-level list styles when having numbered headings AND paragraphs | bwofficer | Word | 2 | 12-12-2014 12:21 AM |