如何在不使用选择的情况下更改当前段落的格式

时间:2019-06-16 15:49:15

标签: vba ms-word

我有以下代码,没有使用Selection

Sub Format paragraph()

Dim wdDoc As Document

    With wdDoc.Range.Find 
       .Font.Size = 12
       .Text = "?" 
       .Execute 
    End With
End Sub

找到字体大小为12的字符时,如何更改当前段落的格式?例如:

wdDoc.Paragraph(current).Font.Size = 14

wdDoc.Paragraph(current).Font.Color = wdBlue

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

诀窍是使用特定的Range对象,该对象可用于访问其“父”段落。当Find.Execute成功时,正在搜索的Range包含找到的项目(与选择跳转到找到的项目一样)。例如:

Sub Format paragraph()
  Dim rng as Range, para as Paragraph
  Dim wdDoc As Document

  Set wdDoc = ActiveDocument. 'Missing in code in question...
  Set rng = wdDoc.Content 'Content returns the Range
    With rng.Find 
       .Font.Size = 12
       .Text = "?" 
       If .Execute = True Then
         Set para = rng.Paragraphs(1)
         para.Font.Size = 14
         para.Font.Color = wdBlue
       End If
    End With
End Sub