如何在Word-VBA中的范围/选择内替换多个表和文本样式?

时间:2017-04-06 08:48:43

标签: vba word-vba

所以,我正在使用VBA上的一个单词模板,每个项目(本例中的要求)包含一个具有不同规范的表(所有表格格式相同)和其他一些信息。在每个表格下方,我都有一个文本,显示每个项目的状态,如:状态:已批准或工作,或拒绝等。我被要求删除模板中的所有其他状态,并仅保留“已拒绝”状态和整个信息和表格具有此状态以浅灰色格式。有没有人知道如何导航到所有表格,信息,并指定我需要格式化的部分?我对此非常陌生,而且我完全陷入困境!这是我写的一些代码:

Sub DeleteWorkflow()

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Normal")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = False
With Selection.Find.Replacement.ParagraphFormat
    .SpaceBefore = 0
    .SpaceBeforeAuto = False
    .SpaceAfter = 0
    .SpaceAfterAuto = False
End With
With Selection.Find
    .Text = "Status: Approved"
    .Text = "Status: Work"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With


Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.Execute

'查找状态“已拒绝”并更改字体颜色

Selection.Find.ClearFormatting
With Selection.Find
        .Text = "Status: Rejected"
        .Forward = True
        .Wrap = Word.WdFindWrap.wdFindContinue
        .Font.ColorIndex = wdGray50
Selection.Find.Execute

End With

找到被拒绝状态并更改其颜色的代码无效,我不明白为什么。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

理念的基础

我们的想法是浏览sentences of the word document。句子包括常规文本以及表格中包含的文本。

当您在VBA中加载单个对象中的所有句子时,您可以按句子查看文档句子的内容并对其执行操作。

如果文档中包含的文字符合您想要的字符,我们也可以将该类型的搜索应用于文档中的tables

代码

对于句子

Sub SENTENCE_CHANGE_COLOR()

Dim i As Long
Dim oSentences As Sentences
'Here we instantiate the variable oSentences to store all the values of the current opened document
Set oSentences = ThisDocument.Sentences

' We loop through every fields of the document
For i = 1 To oSentences.Count
    ' The property .Text contains the text of the item in it
    ' Then we just have to look for the text within the string of characters
    If InStr(oSentences.Item(i).Text, "Status: Rejected") Then
        'Do some stuff, like changing the color
        oSentences.Item(i).Font.ColorIndex = wdGray50
    else
        ' Do some other things like changing the color to a different color
        oSentences.Item(i).Font.ColorIndex = wdGray25
    End If
Next i

End Sub

表格

Sub TABLE_CHANGE_COLOR()

Dim i As Long
Dim oTables As Tables
'Here we instantiate the variable oTables to store all the tables of the current opened document
Set oTables = ThisDocument.Tables

' We loop through every fields of the document
For i = 1 To oTables.Count
    ' Finding the occurence of the text in the table
    If Not InStr(oTables.Item(i).Range.Text, "Status: Rejected") = 0 Then
        'Do some stuff, like changing the color
        oTables.Item(i).Range.Font.ColorIndex = wdGray50
    End If
Next i

End Sub

上述方法的组合

在我们发现“状态:被拒绝”文档的出现后,我们可以通过比较表的结束与发生的开始来选择它之前的表。 请注意,因为以下代码会在“状态:拒绝”之前修改任何表格。因此,如果在不正确的位置输入“Status:rejected”,它将修改上一个表,无论该表在文档中的位置。

Sub REJECTED_TABLE_CHANGE_COLOR()

Dim i As Long, j As Long
Dim oSentences As Sentences
Dim oTables As Tables

'Here we instantiate the variable oSentences to store all the values of the current opened document
Set oSentences = ThisDocument.Sentences
'Here we instantiate the variable oTables to store all the tables of the current opened document
Set oTables = ThisDocument.Tables

' We loop through every fields of the document
For i = 1 To oSentences.Count
    ' The property .Text contains the text of the item in it
    ' Then we just have to look for the text within the string of characters
    If InStr(oSentences.Item(i).Text, "Status: Rejected") Then
        ' When we have found the correct text, we try to find the table just above it
        ' We start from the last table
        ' This condition ensures we do not start looking for before the first table
        If oTables.Item(1).Range.End < oSentences.Item(i).Start Then
            j = oTables.Count
            While oTables.Item(j).Range.End > oSentences.Item(i).Start
                j = j - 1
            Wend
            oTables.Item(j).Range.Font.ColorIndex = wdGray50
        End If
    End If
Next i

End Sub

当在项目中找到匹配条件时,此解决方案将为您提供编辑文档的基础。