vba 使用宏在 Word 文档中查找具有不同格式的文本块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5021516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding block of text with different formatting in Word document using Macros
提问by James Skemp
I'm working on creating a macro in Microsoft Word (2007) for a document that contains text such as this:
我正在为包含以下文本的文档在 Microsoft Word (2007) 中创建宏:
(1) Bold heading.Normal text.
(1) 粗体标题。普通文本。
With this text I'd like to perform a number of transformations upon the first part - (1) Bold heading.- of that text.
对于这篇文章,我想对第一部分进行一些转换 - (1) 粗体标题。- 那个文本。
While "(1)" and "Bold heading." have a consistent style (bold and Arial), the space betweenthe two does not (it's Times New Roman, non-bold).
而“(1)”和“粗体标题”。有一致的风格(粗体和宋体),两者之间没有空格(这是 Times New Roman,非粗体)。
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.
(1)普通文本。
For blocks like this, I want to completely skipthe 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.
但是,我需要能够在搜索中获取两个不同格式的项目以规范该空间,根据我对 VBA 的有限了解,这似乎是不可能的。
Is there a way to find text with different formatting, in a Word macro?
有没有办法在 Word 宏中查找具有不同格式的文本?
Thanks!
谢谢!
回答by Fionnuala
I wonder if something like this would suit:
我想知道这样的事情是否适合:
Dim s As Range
Dim wd As Range
Dim BoldHead As Boolean
Dim doc As Document
Set doc = Word.Documents("Doc2.doc")
For Each s In doc.Sentences
If s.Words(1).Bold = True Then
BoldHead = True
For Each wd In s.Words
If Trim(wd) <> vbNullString _
And wd <> "." _
And wd.Bold = False Then
BoldHead = False
End If
Next
If BoldHead Then
Debug.Print s
End If
End If
Next
Note that Word has a nasty enough habit of not counting the numbers, it sees them as automatic.
请注意,Word 有一个非常讨厌的习惯,即不计算数字,它认为它们是自动的。
回答by James Skemp
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:
Remou 的回答正是我所需要的,但由于 StackOverflow 是一个很好的资源,这就是我最终针对我们的特定情况对其进行调整的内容:
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.)
(下面的一些评论包含在我发现的外部资源中,所以它们是否真的有必要值得怀疑,但是......它有效。)
' Do our bold heading replacements
Dim s As Range, p As Paragraph
Dim wd As Range
Dim BoldHead As Boolean
Dim doc As Document
Set doc = ActiveDocument
For Each p In doc.Paragraphs
Set s = p.Range.Sentences(1)
If s.Words(1).Bold = True And s.Words(1).Characters(1) = "(" Then
BoldHead = True
For Each wd In s.Words
If Trim(wd) <> vbNullString _
And wd <> "." _
And wd.Bold = False Then
BoldHead = False
End If
Next
If BoldHead Then
With s.Find
' Clear all previously set formatting for Find dialog box.
.ClearFormatting
.Text = "(\([0-9]@\)) (?@)."
' Clear all previously set formatting for Replace dialog box.
.Replacement.ClearFormatting
.Replacement.Text = " ."
.Replacement.Font.SmallCaps = True
.Replacement.Font.Name = "Times New Roman"
.Replacement.Font.Bold = False
' The following parameters must be set as follows to find only text formatted for the specified font.
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceOne
End With
With s.Find
' Clear all previously set formatting for Find dialog box.
.ClearFormatting
.Text = "(\([0-9]@\)) "
' Clear all previously set formatting for Replace dialog box.
.Replacement.ClearFormatting
.Replacement.Text = "" & vbTab
.Replacement.Font.SmallCaps = False
.Replacement.Font.Name = "Arial"
.Replacement.Font.Bold = True
' The following parameters must be set as follows to find only text formatted for the specified font.
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceOne
End With
End If
End If
Next