Word VBA突出显示文本

时间:2018-04-23 13:49:45

标签: vba ms-word highlighting

我在Microsoft Word中生成一些安全报告 - 导入SOAP xml请求和响应......

我想尽可能地自动化这个过程,我需要在这些请求/响应中突出显示一些文本。怎么做?一般来说,我需要强调请求中的非标准输入(每次不同 - 不良数据类型等)和响应中的错误字符串(大多数看起来像这样<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

它突出了第一个故障串,但其他外观保持未格式化,我不知道为什么....感谢您的回复。

1 个答案:

答案 0 :(得分:1)

执行此操作的最有效方法是使用多个Range对象。可以认为Range就像一个不可见的选择,重要的区别在于,虽然只有一个Selection对象,但代码中可能有多个Range个对象。

我已经调整了你的代码,添加了三个Range个对象:一个用于整个文档;一个用于查找起始标签;一个用于查找结束标记。 Duplicate属性用于从一个Range“复制”另一个Set(这是由于Word Range一个If到另一个Execute的奇怪之处(链接它们)。

为清楚起见,我还为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 获取“成功”比在事后依赖split = input_values.split() id1 = int(split[0]) id2 = int(split[-1]) name = " ".join(split[1:-1]) 更可靠。

var timer = duration, minutes, seconds;
相关问题