Vba MS Word set OutlineDemote为特定单词后的文本

Vba MS Word set OutlineDemote为特定单词后的文本,vba,ms-word,Vba,Ms Word,我想将word文件中特定单词后的整个文本设置为OutlineDemote 我的VBA脚本正在将整个文档设置为OutlineDemote 我做错了什么 Sub FormatFeatures() Dim myRange As Range Set myRange = ActiveDocument.Content myRange.Find.Execute FindText:="Test", _ Forward:=True If myRange.Find.Found =

我想将word文件中特定单词后的整个文本设置为OutlineDemote

我的VBA脚本正在将整个文档设置为OutlineDemote

我做错了什么

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

找到的范围的末尾在一个段落内。因此,您需要从以下段落的开头开始使用范围:

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
编辑: 结果截图


我是MSWord VBA的新手。你能解释一下为什么是
Start:=myRange.Next(wdparagration).Start
End:=ActiveDocument.Content.End
而不是
End:=myRange.Next(wdparagration).End
?@chockledson-“将word文件中特定单词后的整个文本设置为OutlineDemote”-这样就可以包括文档中的所有剩余段落,就像问题中OP的代码一样。您的建议只会将查找到的文本后面的单个段落降级。但我的问题是,此代码将整个文档设置为OutlineDemote…我做错了什么?@Andreas-“我想将word文件中特定单词后面的整个文本设置为OutlineDemote”表示要将文档中找到的文本后的所有剩余段落降级。如果这不是你想要的,那么你需要清楚地解释你想要发生的事情。亲爱的@Timothyrylat你是对的,我只想在找到的单词后设置文本,但是我的代码以及你的代码将我的整个文档设置为大纲降级