Creating a new style will accomplish what you want. Try this procedure:
- Use the Paragraphs dialog box to make
Word
insert a page break before all Heading 2 text.
- Create a new style, "Heading 2 Prime", that's exactly like Heading 2, but uncheck the "page break before" option. Be sure you spell "Heading 2 Prime" exactly as written here.
- Press Alt+F11 to open the
VBA development environment
, then click "Insert > Module" to open a new window for entering programs.
- Paste the following statements into the window. These statements replace all Heading 2 paragraphs that come after Heading 1 paragraphs, with the Heading 2 Prime style.
Code to paste:
'''''''''''''''''''''''''''''''''''''''''''
Sub replace_Heading2_with_Heading2Prime()
Dim i As Integer
Dim s As String
Dim h As String
'''''''''''''''''''''''''''''''''''''
'loop through all pgp and report heading 2
For i = 1 To ActiveDocument.Paragraphs.Count
s = ActiveDocument.Paragraphs(i).Style
If (s = "Heading 2") Then
h = findPriorHeading(i - 1)
If (h = "Heading 1") Then
ActiveDocument.Paragraphs(i).Style = "Heading 2 Prime"
End If
End If
Next i
Exit Sub
End Sub
Function findPriorHeading(iPgp As Integer) As Variant
Dim i As Integer
Dim blnFoundHeading As Boolean
'walk backwards until any heading is found
With ActiveDocument
i = iPgp
blnFoundHeading = False
Do Until (i < 1 Or blnFoundHeading)
s = .Paragraphs(i).Style
If (InStr(s, "Heading") > 0) Then
blnFoundHeading = True
findPriorHeading = s
Exit Function
End If
i = i - 1
Loop
End With
findPriorHeading = ""
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Press Alt+F11 to return to Word
, then press Alt+F8 to open the Macros dialog box. Double click the "replace_Heading2_with_Heading2Prime" item to run the macro.
I hope this helps.
Word's TOC field can gather content for the ToC from multiple sources.
One source is "paragraphs whose styles have the Outline Levels specified by the \o parameter, so e.g.
{ TOC \o "1-2" }
Would give you a ToC with Heading 1, heading 2, and any other paragraphs whose styles have Outline Levels 1 or 2. (In other words, the current Outline Level of the paragraph is not relevant, AFAIK). In this case, a paragraph whose style has Outline Level n will appear at level n in the ToC.
You can set the Outline Level of the style by finding the style name in the style gallery in the Home tab, right-clicking, then selecting Modify... Then click the Format... dropdown at the bottom left and click Paragraph. The item you need is the "Outline Level" (which you cannot change for the built-in Heading n styles). You may not be able to modify the Outline Level if there are no paragraphs with that style in the document.
Another source is a list of specified style names using the \t parameter. This allows you to use styles that have no Outline Level. So if you want Heading 1, Heading 2, Heading 3 at ToC levels 1, 2 and 3, and "mystyle1" to be shown at ToC level 2 and "mystyle2" to be at ToC level 3, you can use
{ TOC \o "1-3" \t "mystyle1,2,mystyle2,3" }
If your Regional Settings specify ";" as the list separator character, you need to use ";" instead of "," in there.
You can also use the \t parameter to modify the ToC level where you want paragraphs with style Heading 1 etc.
These features are available from the standard ToC insertion dialog box (References->Table of Contents->Custom table of contents...->Options)
Best Answer
Pandoc
You can write your Markdown text as usual, then use Pandoc to create an RTF file, which can easily be opened and edited in Word.
Pandoc is free, open source and comes for Windows, OS X, Linux and BSD. Check the user's guide here.
For example, use:
… then open it in Word and style as you want. The
-s
option needs to be used to produce a standalone file. HTML should retain the syntactical "heading" elements, whereas RTF and ODT only style headings.