gpt4 book ai didi

vba - 在 Word 文档的标题部分中搜索文本

转载 作者:行者123 更新时间:2023-12-04 05:58:44 26 4
gpt4 key购买 nike

我想确认一个文档是否包含一些文本,唯一的问题是这个文本在标题中。这是我正在使用的代码,即使文本存在,它也会不断返回 false:

Set CurrentDoc = Documents.Open("a.doc")

With CurrentDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Find
.Text = "This is the text to find"
.Forward = True
.Execute
If (.Found = True) Then Debug.Print "Match"
End With

以下似乎也不起作用(我假设 .Content 不包括页眉/页脚):
With CurrentDoc.Content.Find
.Text = "This is the text to find"
.Forward = True
.Execute
If (.Found = True) Then Debug.Print "Match"
End With

任何帮助将不胜感激。

最佳答案

您可能正在尝试在错误的部分/标题类型中进行搜索。你可以试试这个代码:

Dim rng As Range
Dim intSecCount As Integer
Dim intHFType As Integer
intSecCount = ActiveDocument.Sections.Count
For intSection = 1 To intSecCount
With ActiveDocument.Sections(intSection)
For intHFType = 1 To 3
Set rng = ActiveDocument.Sections(intSection).Headers(intHFType).Range
rng.Find.Execute findtext:="This is the text to find", Forward:=True
If rng.Find.Found = True Then
Debug.Print "Match"
End If
Next intHFType
End With
Next intSection

关于vba - 在 Word 文档的标题部分中搜索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9186629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com