Vba 使用宏在Word文档中查找格式不同的文本块
我正在Microsoft Word 2007中为包含以下文本的文档创建宏: 1粗体标题。普通文本 在本文中,我想对第一部分-1粗体标题进行一些转换对该文本的修改 而1和粗体标题。有一个统一的风格粗体和Arial,两者之间的空间不它的时代新罗马,非粗体 我想搜索下面的内容可以,没有任何格式限制Vba 使用宏在Word文档中查找格式不同的文本块,vba,ms-word,Vba,Ms Word,我正在Microsoft Word 2007中为包含以下文本的文档创建宏: 1粗体标题。普通文本 在本文中,我想对第一部分-1粗体标题进行一些转换对该文本的修改 而1和粗体标题。有一个统一的风格粗体和Arial,两者之间的空间不它的时代新罗马,非粗体 我想搜索下面的内容可以,没有任何格式限制 "^13(\([0-9]@\)) (?@)." 不幸的是,在有些情况下,文本如下所示: 1普通文本 对于这样的块,我想完全跳过文本 不幸的是,我的通配符搜索也会找到这些实例,除非我可以通过字体样式进行限制
"^13(\([0-9]@\)) (?@)."
不幸的是,在有些情况下,文本如下所示:
1普通文本
对于这样的块,我想完全跳过文本
不幸的是,我的通配符搜索也会找到这些实例,除非我可以通过字体样式进行限制
如果我可以在第一种情况下规范化空格,那么我可以在通配符搜索中添加字体限制,以获取正确的内容
.Text = "^13(\([0-9]@\)) (?@)."
.Font.Name = "Arial"
.Font.Size = 9
.Font.Bold = True
但是,我需要能够在搜索中抓取两个不同格式的项目,以规范化该空间,从我对VBA的有限知识来看,这似乎是不可能的
有没有办法在Word宏中查找具有不同格式的文本
谢谢 我想知道这样的东西是否合适:
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
请注意,Word有一个不数数的坏习惯,它认为数字是自动的。我想知道这样的习惯是否适合:
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
请注意,Word有一个不数数的坏习惯,它认为这些数字是自动的。Remou的答案正是我所需要的,但由于StackOverflow是一个很好的资源,因此我最终对它进行了调整,以适应我们的特殊情况: 特别是,文本位于段落的第一句话内。不幸的是,这似乎并没有抓住我们所有的案例,但它抓住了大多数案例,并让用户以最快的方式到达那里 下面的一些评论是关于我发现的外部资源的,所以它们是否真的有必要是值得怀疑的,但是。。。它起作用了
' 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 = "\1 \2."
.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 = "\1" & 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
Remou的答案正是我所需要的,但由于StackOverflow是一个很好的资源,因此我最终针对我们的特定案例对其进行了调整: 特别是,文本位于段落的第一句话内。不幸的是,这似乎并没有抓住我们所有的案例,但它抓住了大多数案例,并让用户以最快的方式到达那里 下面的一些评论是关于我发现的外部资源的,所以它们是否真的有必要是值得怀疑的,但是。。。它起作用了
' 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 = "\1 \2."
.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 = "\1" & 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
我不太清楚困难在哪里,你可以逐句或逐字检查文件。也许你可以在你的帖子上加一两行?谢谢。我更新了这个问题,以澄清我想要转换这段文本,并强调我的搜索将找到太多的实例。希望这会有帮助。我不太确定困难在哪里,你可以逐句或逐字检查文档。也许你可以在你的帖子上加一两行?谢谢。我更新了这个问题,以澄清我想要转换这段文本,并强调我的搜索将找到太多的实例。希望这能有所帮助。请原谅我的无知,但既然它是按文件名引用Word文档,这是否意味着我需要创建一个小应用程序来在Word之外运行它?理想情况下,宏最适合将要进行清理的用户。不,一点也不,您可以设置doc=activedocument对不起,响应太晚,我尽量享受假期,但我可以:D。我今天早上尝试了这个,它似乎完全符合我的需要。今天我想用一个更大的文档来测试这个问题,但现在看来这就是解决方案。我最终调整了它,这样我就可以只搜索每个段落的第一句话,但这正是我完成我的问题所需要的。谢谢请原谅我的无知,但由于它是通过文件名引用Word文档,这是否意味着我需要创建一个小应用程序来在Word之外运行它?理想情况下,宏最适合将要进行清理的用户。不,一点也不,您可以设置doc=activedocument对不起,响应太晚,我尽量享受假期,但我可以:D。我今天早上尝试了这个,它似乎完全符合我的需要。今天我想用一个更大的文档来测试这个问题,但现在看来这就是解决方案。我最终调整了它,这样我就可以只搜索每个段落的第一句话,但这正是我完成我的问题所需要的。谢谢