Word VBA 突出显示文本
Word VBA highlighting text
我正在 Microsoft Word 中生成一些安全报告 - 导入 SOAP xml 请求和响应...
我想尽可能地自动化这个过程,我需要突出显示这些 requests/responses 中的一些文本。怎么做?一般来说,我需要突出显示请求中的非标准输入(每次都不同 - 错误的数据类型等)和响应中的错误字符串(大多数看起来像这样 <faultstring>some error</faultstring>
)。
这是我正在尝试的代码:
Sub BoldBetweenQuotes()
' base for a quotes finding macro
Dim blnSearchAgain As Boolean
' move to start of doc
Selection.HomeKey Unit:=wdStory
' start of loop
Do
' set up find of first of quote pair
With Selection.Find
.ClearFormatting
.Text = "<faultstring>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Execute
End With
If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
' switch on selection extend mode
Selection.Extend
' find second quote of this pair
Selection.Find.Text = "</faultstring>"
Selection.Find.Execute
If Selection.Find.Found Then
Selection.MoveLeft Unit:=wdCharacter, Count:=Len(Selection.Find.Text)
' make it bold
Selection.Font.Bold = True
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveRight Unit:=wdCharacter, Count:=1
blnSearchAgain = True
Else
blnSearchAgain = False
End If
Else
blnSearchAgain = False
End If
Loop While blnSearchAgain = True
End Sub
它只突出显示了第一个故障字符串,但其他出现的情况仍未格式化,我不知道为什么....感谢您的回复。
最有效的方法是处理多个 Range
对象。将 Range
想象成一个不可见的选择,重要的区别是,虽然只能有一个 Selection
对象,但您的代码中可以有多个 Range
对象。
我已经调整了您的代码,添加了三个 Range
对象:一个用于整个文档;一个用于整个文档;一个用于查找起始标签;一个用于查找结束标记。 Duplicate
属性 用于 "copy" 一个 Range
来自另一个(这是由于 Word 中的一个奇怪之处,如果你 Set
一个 Range
到另一个,链接它们)。
为了清楚起见,我还为您的 If
比较添加了几个布尔测试值。根据我的经验,直接从 Execute
获取 "success" 比事后依赖 Find.Found
更可靠。
Sub BoldBetweenQuotes()
' base for a quotes finding macro
Dim blnSearchAgain As Boolean
Dim blnFindStart As Boolean
Dim blnFindEnd As Boolean
Dim rngFind As word.Range
Dim rngFindStart As word.Range
Dim rngFindEnd As word.Range
Set rngFind = ActiveDocument.content
Set rngFindStart = rngFind.Duplicate
Do
' set up find of first of quote pair
With rngFindStart.Find
.ClearFormatting
.Text = "<faultstring>"
.Replacement.Text = ""
.Forward = True
.wrap = wdFindStop
blnFindStart = .Execute
End With
If blnFindStart Then
rngFindStart.Collapse wdCollapseEnd
Set rngFindEnd = rngFindStart.Duplicate
rngFindEnd.Find.Text = "</faultstring>"
blnFindEnd = rngFindEnd.Find.Execute
If blnFindEnd Then
rngFindStart.End = rngFindEnd.Start
' make it bold
rngFindStart.Font.Bold = True
rngFindStart.Start = rngFindEnd.End
rngFindStart.End = rngFind.End
blnSearchAgain = True
Else
blnSearchAgain = False
End If
Else
blnSearchAgain = False
End If
Loop While blnSearchAgain = True
End Sub
我正在 Microsoft Word 中生成一些安全报告 - 导入 SOAP xml 请求和响应...
我想尽可能地自动化这个过程,我需要突出显示这些 requests/responses 中的一些文本。怎么做?一般来说,我需要突出显示请求中的非标准输入(每次都不同 - 错误的数据类型等)和响应中的错误字符串(大多数看起来像这样 <faultstring>some error</faultstring>
)。
这是我正在尝试的代码:
Sub BoldBetweenQuotes()
' base for a quotes finding macro
Dim blnSearchAgain As Boolean
' move to start of doc
Selection.HomeKey Unit:=wdStory
' start of loop
Do
' set up find of first of quote pair
With Selection.Find
.ClearFormatting
.Text = "<faultstring>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Execute
End With
If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
' switch on selection extend mode
Selection.Extend
' find second quote of this pair
Selection.Find.Text = "</faultstring>"
Selection.Find.Execute
If Selection.Find.Found Then
Selection.MoveLeft Unit:=wdCharacter, Count:=Len(Selection.Find.Text)
' make it bold
Selection.Font.Bold = True
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveRight Unit:=wdCharacter, Count:=1
blnSearchAgain = True
Else
blnSearchAgain = False
End If
Else
blnSearchAgain = False
End If
Loop While blnSearchAgain = True
End Sub
它只突出显示了第一个故障字符串,但其他出现的情况仍未格式化,我不知道为什么....感谢您的回复。
最有效的方法是处理多个 Range
对象。将 Range
想象成一个不可见的选择,重要的区别是,虽然只能有一个 Selection
对象,但您的代码中可以有多个 Range
对象。
我已经调整了您的代码,添加了三个 Range
对象:一个用于整个文档;一个用于整个文档;一个用于查找起始标签;一个用于查找结束标记。 Duplicate
属性 用于 "copy" 一个 Range
来自另一个(这是由于 Word 中的一个奇怪之处,如果你 Set
一个 Range
到另一个,链接它们)。
为了清楚起见,我还为您的 If
比较添加了几个布尔测试值。根据我的经验,直接从 Execute
获取 "success" 比事后依赖 Find.Found
更可靠。
Sub BoldBetweenQuotes()
' base for a quotes finding macro
Dim blnSearchAgain As Boolean
Dim blnFindStart As Boolean
Dim blnFindEnd As Boolean
Dim rngFind As word.Range
Dim rngFindStart As word.Range
Dim rngFindEnd As word.Range
Set rngFind = ActiveDocument.content
Set rngFindStart = rngFind.Duplicate
Do
' set up find of first of quote pair
With rngFindStart.Find
.ClearFormatting
.Text = "<faultstring>"
.Replacement.Text = ""
.Forward = True
.wrap = wdFindStop
blnFindStart = .Execute
End With
If blnFindStart Then
rngFindStart.Collapse wdCollapseEnd
Set rngFindEnd = rngFindStart.Duplicate
rngFindEnd.Find.Text = "</faultstring>"
blnFindEnd = rngFindEnd.Find.Execute
If blnFindEnd Then
rngFindStart.End = rngFindEnd.Start
' make it bold
rngFindStart.Font.Bold = True
rngFindStart.Start = rngFindEnd.End
rngFindStart.End = rngFind.End
blnSearchAgain = True
Else
blnSearchAgain = False
End If
Else
blnSearchAgain = False
End If
Loop While blnSearchAgain = True
End Sub