用vba可以查找word文档中满足格式的内容,如果想要一次性选中所有这些满足格式的内容,可以使用如下的代码:
Sub QQ1722187970()
Const wdReplaceAll = 2
Const wdFindStop = 0
Const wdEditorCurrent = -6
Dim oDoc As Document
Set oDoc = Word.ActiveDocument
oDoc.Windows(1).View.ShadeEditableRanges = False
Dim oEditor As Editor
Dim oRng As Range
Set oRng = Word.Selection.Range
'先判断是否有选中区域,没有选中则表示整个文档
If oRng.Start = oRng.End Then
Set oRng = oDoc.Content
End If
With oRng.Find
.ClearFormatting
'要查找的格式
.Font.Bold = True
.Text = ""
Do
'先执行一次查找
bResult = .Execute(FindText:="", Format:=True)
'如果找到了
If bResult = True Then
'为找到的内容单元格区域添加Editor
oRng.Editors.Add (wdEditorCurrent)
End If
Loop Until bResult = False
End With
'选中所有可以编辑的区域
oDoc.Content.GoToEditableRange(wdEditorCurrent).Editors(1).SelectAll
End Sub


发表评论