我想检查文档的范围并检查短语“form CAPTION”是否存在但不存在“subform CAPTION”。我认为
.MatchWholeWord = True
是做到这一点的关键,但以下代码始终返回 .Found = TRUE
Sub CheckIfForm()
Dim doc As Document
Dim searchRange As Range
Dim searchText As String
' Set the search text
searchText = "form" & String(25, " ") & "CAPTION"
' Set the document and search range
Set doc = ActiveDocument
Set searchRange = doc.Range
' Find the search text
With searchRange.Find
.Text = searchText
.MatchWholeWord = True ' Match only whole words
.MatchCase = False ' Case insensitive
.Wrap = wdFindStop ' Stop at the end of the document
.Execute
' Check if the text is found
If .Found Then
MsgBox "The phrase """ & searchText & """ was found.", vbInformation
Else
MsgBox "The phrase """ & searchText & """ was not found.", vbExclamation
End If
End With
End Sub
MatchWildcards
方法上使用 Find
选项Sub HighlightWordsWithWildCards()
Application.ScreenUpdating = False
ActiveDocument.Content.HighlightColorIndex = wdNoHighlight
Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "<form CAPTION>" ' **
.MatchWildcards = True ' **
.Forward = True
.Wrap = wdFindContinue
With .Replacement
.Text = "^&"
.ClearFormatting
.Highlight = True
End With
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub