0
votes

So, I am working with VBA on a word template which for every item (requirements in this case) contains a table with different specifications (all the tables are in the same format) and some other information. Below each table I have a text which shows the status of each item like: status: Approved or Work, or Rejected etc. I am asked to delete all the other statuses in the template and keep only the "Rejected" status and the whole information and table with that has this status to format in a light grey. Does anybody has any idea how to navigate to all tables, information, and specify the section I need to Format? I am very new to this and I am completely stucked! Here's some code I wrote:

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

'Finds status "Rejected" and changes the font color

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

End With

The code to find the rejected status and to change its color is not working and I am not getting it why. Any idea?

1
Would you have a sample of what the document looks like or an example we could see to find a way to work around it ? - Pierre Chevallier
@Pierre I do not know why I can not provide an screenshot of a sample now but I will try to describe it as good as I can. So, just imagine a 4 column table regarding the item than after the table, below, on the left side the ID of the Item and on the right the status, eg: Status: Rejected. - user7562453
Next lines are some text and this is repetitive for the other items which have the same or other statuses like work, in Analysis, approved etc. I should Keep only the status rejected on the template and delete the others. All the Information with rejected status should be formatted in Grey. I hope I was clear and thanks for your time :) @PierreChevallier - user7562453

1 Answers

0
votes

Basis of the idea

The idea is to look through the sentences of the word document. Sentences comprise regular text and also text contained within tables.

As you load all the sentences in a single object in VBA, you can look through the content of the document sentences by sentences and perform an action on it.

We can also apply that type of search to tables within the document, if the text they contain match the characters you want.

The code

For sentences

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

For tables

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

Combination of the above methods

After we found the occurrence of a "Status: Rejected" document we can select the table right before it by comparing the table's end to the start of the occurrence. Beware since the following code would modify any table before "Status: rejected". So if "Status: rejected" is input in an incorrect location, it will modify the previous table wherever this table will be in the document.

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

This solution would provide you the basis to edit the document when the matching criteria is found within an item.