如何在选定的文本中或从文档的特定点搜索粗体段落?

时间:2019-10-31 12:44:44

标签: vba ms-word

我有以下代码可以删除整个文档中所有粗体段落之后的段落。如何修改它,使其仅影响某些文本,特别是从光标所处的某个点到文档末尾?谢谢

Dim para As Paragraph

For Each para In ActiveDocument.Paragraphs
    If para.Range.Font.Bold = True Then para.Next.Range.Delete
Next para

1 个答案:

答案 0 :(得分:0)

要限制搜索范围,您需要将ActiveDocument替换为要搜索的范围。每当您在Word中编写代码时,最好避免使用Selection对象。使用Selection在屏幕上四处移动光标,这意味着每次必须重新绘制屏幕。通过使用Range对象可以避免这种情况。

  Dim para As Paragraph
  Dim searchRange As Range

  'pick up the start of the range
  Set searchRange = Selection.Range
  'move the end of the range to the end of the document
  searchRange.End = ActiveDocument.Content.End
  For Each para In searchRange.Paragraphs
    If para.Range.Font.Bold = True Then para.Next.Range.Delete
  Next para