0
votes

The task is to assign the current heading to a Headings which is 1 level lower that the previous heading.

The problem is that the previous heading changes from section to section, depending on the document.

Currently I have the following:

'Setting a Temp Bookmark to come back to
ActiveDocument.Bookmarks.Add Name:="CurrentPosition", Range:=Selection.Range

'Going Back to the previous Heading
Selection.GoTo What:=wdGoToBookmark, Name:="PreviousHeading"

'Determining the current and assigning the next style
If Selection.Style = ActiveDocument.Styles("Heading 1") Then
    NextHeadingStyle= ActiveDocument.Styles("Heading 2")
ElseIf Selection.Style = ActiveDocument.Styles("Heading 2") Then
    NextHeadingStyle= ActiveDocument.Styles("Heading 3")
ElseIf Selection.Style = ActiveDocument.Styles("Heading 3") Then
    NextHeadingStyle= ActiveDocument.Styles("Heading 4")
ElseIf Selection.Style = ActiveDocument.Styles("Heading 4") Then
    NextHeadingStyle= ActiveDocument.Styles("Heading 5")
ElseIf Selection.Style = ActiveDocument.Styles("Heading 5") Then 
    NextHeadingStyle= ActiveDocument.Styles("Heading 6")
End If

'Going back to the new Heading
Selection.GoTo What:=wdGoToBookmark, Name:="CurrentPosition"

'Changing the Style accordingly
Selection.Style = NextHeadingStyle  

As a test, something which i have tried is:

 Selection.Style = wdStyleHeading4 - 1

This will then set the text to a "Heading 5"

What I would like is something like:

'Setting a Temp Bookmark to come back to
ActiveDocument.Bookmarks.Add Name:="CurrentPosition", Range:=Selection.Range

'Going Back to the previous Heading
Selection.GoTo What:=wdGoToBookmark, Name:="PreviousHeading"

NextHeadingStyle = Selection.Style
NextHeadingStyle = NextHeadingStyle - 1

'Going back to the new Heading
Selection.GoTo What:=wdGoToBookmark, Name:="CurrentPosition"

'Changing the Style accordingly
Selection.Style = NextHeadingStyle

If there a way to do this?

1

1 Answers

1
votes

Selection.Paragraphs.OutlineDemote

This is the equivalent of pressing Alt+Shift+Right Arrow in the Word UI.

Note that there's also the opposite: OutlinePromote. And there's OutlineDemoteToBody which removes the Heading formatting and applies the Normal style.

These all originated working in the Outline View, but also work in Print Layout.

EDIT to address knowing which Heading level precedes so as to decide whether to Demote/Promote or use same level:

The following returns the style of the Heading Level just above the current selection.

Document.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.Style