找到唯一的文本,然后注明页码



使用.Find在Word文档中定位文本,我如何找到该文本在哪个页面上?

我有一个装运标签的Word文档,每页一个。我想在这个文件中搜索一个特定的订单号,并记下文件名和页码。这样,当我的应用程序(使用Word对象库的MS Access)稍后向客户发送订单时,它可以自动打开正确的标签文件并打印该订单的标签。

下面的简化代码。找工作(查找。Found = True)。该代码返回文档的最后一页。如果我在Word文档可见的情况下运行代码,我可以看到,尽管.Find.Execute正在工作,但文档没有移动到正确的页面。

Dim Find As Word.Find
Dim LabelPageNum as Integer

LabelPageNum = 0
Set Find = Word.ActiveDocument.Content.Find

Find.ClearFormatting
Find.Text = "ABC-123456"
Find.Forward = True
Find.Wrap = wdFindContinue
Find.Format = False
Find.MatchWildcards = True
Find.IgnoreSpace = True
Find.IgnorePunct = True
Find.Execute

If Find.Found Then
LabelPageNum = Word.ActiveDocument.Content.Information(wdActiveEndPageNumber)
End if

Find.Parent.Information(wdActiveEndPageNumber)将给您结果。Find.Parent返回已找到的范围-并在此基础上应用Information

BTW:不使用码字是很好的编码习惯。作为变量名

If Find.Found Then LabelPageNum = .Information(wdActiveEndPageNumber)

相关内容

最新更新