Andreas
Andreas

Reputation: 9

MS Word set OutlineDemote to text after specific word

I want to set the whole text after a specific word in a word file to OutlineDemote.

My VBA script is setting the whole document to OutlineDemote.

What am i doing wrong?

Sub FormatFeatures()

Dim myRange As Range
Set myRange = ActiveDocument.Content

myRange.Find.Execute FindText:="Test", _
    Forward:=True

If myRange.Find.Found = True Then
    myRange.SetRange (myRange.End), ActiveDocument.Content.End
    myRange.Paragraphs.OutlineDemote
End If
End Sub

Upvotes: 0

Views: 49

Answers (1)

Timothy Rylatt
Timothy Rylatt

Reputation: 7860

The end of the found range is within a paragraph. So you need to start the range you're working with from the beginning of the following paragraph:

Sub FormatFeatures()

   Dim myRange As Range
   Set myRange = ActiveDocument.Content

   myRange.Find.Execute FindText:="Test", _
      Forward:=True

   If myRange.Find.Found = True Then
      myRange.SetRange myRange.Next(wdParagraph).Start, ActiveDocument.Content.End
      myRange.Paragraphs.OutlineDemote
   End If
End Sub

EDIT: Screenshot of results

enter image description here

Upvotes: 1

Related Questions