使用宏在Word文档中查找不同格式的文本块
我正在努力在 Microsoft Word (2007) 中为包含如下文本的文档创建宏:
(1) 粗体标题。 普通文本。
对于此文本,我想对该文本的第一部分 - (1) 粗体标题。 执行一些转换。
而“(1)”和“粗体标题”。具有一致的风格(粗体和 Arial),两者之间的空格没有(它是 Times New Roman,非粗体)。
我认为搜索以下内容是可行的,没有任何格式限制。
"^13(\([0-9]@\)) (?@)."
不幸的是,也存在文本如下所示的情况:
(1) 普通文本。
对于这样的块,我想完全跳过文本。
不幸的是,我的通配符搜索也会找到这些实例,除非我可以通过字体样式限制它。
如果我可以规范第一种情况下的空格,那么我可以在通配符搜索中添加字体限制以获取正确的内容。
.Text = "^13(\([0-9]@\)) (?@)."
.Font.Name = "Arial"
.Font.Size = 9
.Font.Bold = True
但是,我需要能够在搜索中抓取两个不同格式的项目来标准化该空间,但根据我有限的 VBA 知识,这似乎是不可能的。
有没有办法在Word宏中查找不同格式的文本?
谢谢!
I'm working on creating a macro in Microsoft Word (2007) for a document that contains text such as this:
(1) Bold heading. Normal text.
With this text I'd like to perform a number of transformations upon the first part - (1) Bold heading. - of that text.
While "(1)" and "Bold heading." have a consistent style (bold and Arial), the space between the two does not (it's Times New Roman, non-bold).
I thought a search for the below would work, without any format restrictions.
"^13(\([0-9]@\)) (?@)."
Unfortunately, there's also cases where text is as follows:
(1) Normal text.
For blocks like this, I want to completely skip the text.
Unfortunately, my wildcard search is going to find these instances too, unless I can restrict it by font styles.
If I could normalize the space in the first case, then I could add the Font restrictions on my wildcard search to grab the correct content.
.Text = "^13(\([0-9]@\)) (?@)."
.Font.Name = "Arial"
.Font.Size = 9
.Font.Bold = True
But, I'd need to be able to grab two differently formatted items in a search to normalize that space, which, from my limited knowledge of VBA, doesn't appear to be possible.
Is there a way to find text with different formatting, in a Word macro?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想知道这样的东西是否适合:
请注意,Word 有一个非常讨厌的习惯,即不计算数字,它认为它们是自动的。
I wonder if something like this would suit:
Note that Word has a nasty enough habit of not counting the numbers, it sees them as automatic.
Remou 的答案正是我所需要的,但由于 StackOverflow 是一个很棒的资源,因此我最终针对我们的特定情况对其进行了调整:
特别是,文本位于段落的第一句话内。不幸的是,这似乎并没有涵盖我们所有的情况,但它抓住了大多数情况,并让用户大部分到达那里。
(下面的一些评论包含在我发现的外部资源中,因此它们是否真的有必要值得怀疑,但是......它有效。)
Remou's answer is exactly what I needed, but since StackOverflow is a great resource, this is what I ended up tweaking it to for our particular case:
In particular, the text is within the first sentence of a paragraph. Unfortunately this doesn't seem to catch all of our cases, but it grabs most of them, and gets the user most of the way there.
(Some of the comments below were included on external resources I'd found, so whether they're actually necessary is questionable, but ... it works.)