View Single Post
 
Old 12-18-2018, 05:06 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The code posted does not address the font colour of the four total fields
Code:
    ActiveDocument.SelectContentControlsByTitle("Not Selected").Item(1).Range.Text = lngNullorInvalid
    ActiveDocument.SelectContentControlsByTitle("Yes").Item(1).Range.Text = lngY
    ActiveDocument.SelectContentControlsByTitle("No").  Item(1).Range.Text = lngN
    ActiveDocument.SelectContentControlsByTitle("NA").  Item(1).Range.Text = lngNA
You can modify the code to colour the fonts in those ranges e.g.
Code:
Sub UpdateTotals()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 12/2/2018
'Graham Mayor - https://www.gmayor.com - Last updated - 18 Dec 2018

Dim lngY As Long, lngN As Long, lngNA As Long, lngNullorInvalid As Long
Dim oCC As ContentControl
Dim oRng As Range
    For Each oCC In ActiveDocument.Range.ContentControls
        If oCC.Type = 3 Or oCC.Type = 4 Then
            Select Case oCC.Range.Text
                Case Is = "Y": lngY = lngY + 1
                Case Is = "N": lngN = lngN + 1
                Case Is = "NA": lngNA = lngNA + 1
                Case Else: lngNullorInvalid = lngNullorInvalid + 1
            End Select
        End If
        DoEvents
    Next oCC

    Set oRng = ActiveDocument.SelectContentControlsByTitle("Not Selected").Item(1).Range
    oRng.Text = lngNullorInvalid
    oRng.Font.ColorIndex = wdRed
    Set oRng = ActiveDocument.SelectContentControlsByTitle("Yes").Item(1).Range
    oRng.Text = lngY
    oRng.Font.ColorIndex = wdGreen
    Set oRng = ActiveDocument.SelectContentControlsByTitle("No").Item(1).Range
    oRng.Text = lngN
    oRng.Font.ColorIndex = wdBlue
    Set oRng = ActiveDocument.SelectContentControlsByTitle("NA").Item(1).Range
    oRng.Text = lngNA
    oRng.Font.ColorIndex = wdDarkYellow


lbl_Exit:
    Set oRng = Nothing
    Set oCC = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote