如何找到 MS Word 文档中突出显示的文本块的数量

发布于 2024-08-30 03:11:30 字数 140 浏览 0 评论 0原文

我正在 MS Word 中编写一个宏,它应该找到文档中所有突出显示的文本并对每个文本执行一些操作。我正在计划一个循环来执行搜索和操作部分,并且这部分代码没有问题。
但我不知道如何找到我需要多少次迭代。有没有办法确定VBA中突出显示的数量?
非常感谢。

I am writing a macro in MS Word which should find all highlighted text in a document and perform some action on each. I am planning a loop to do the search and manipulation part and have no problem with this portion of the code.
But I don't know how to find how many iterations I'm going to need. Is there a way to determine the number of highlights in VBA?
Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2024-09-06 03:11:31
With ActiveDocument.Range.Find
  .Highlight = True
  While .Execute
    Debug.Print .Parent.Text
  Wend
End With

无需预先计算匹配数。您可以循环执行搜索,一旦没有更多匹配项,搜索就会停止。

确保将搜索应用到文档的正确部分。我使用了 ActiveDocument.Range,但任何 Range 对象都可以。也许更具体的东西更适合您的情况。

另外,检查 Find 对象的许多其他属性并将它们设置为合理的值,这比使用默认值要好(没有人能记住所有选项的所有默认值,加上 Find 对象可能已通过某些早期搜索设置)。

With ActiveDocument.Range.Find
  .Highlight = True
  While .Execute
    Debug.Print .Parent.Text
  Wend
End With

There's no need to calculate the number of matches up-front. You can execute the search in a loop and it will stop once there are no more matches.

Make sure you apply the search to the right part of the document.I used ActiveDocument.Range, but any Range object will do. Maybe something more specific is better for your case.

Also, check out the many other properties of the Find object and set them to sensible values, this is better than going with the defaults (nobody can remember all defaults for all options, plus the Find object might already be set up by some earlier search).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文