Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Content Controls In Math Applications
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

This Microsoft Word Help & Tips page provides examples and shows you how to perform basic mathematical calculations with content controls using the document ContentControlOnExit event. For an introduction to content controls and links to my other tips pages relating to content controls see: Content Controls.

Simple Addition

You can use a group of plain text content controls to define the variables for addition
(e.g., A + B + C) and place sum in separate content control as shown below:

cc math 1
  1. Open a new Word document and save it using the macro enable extension "docm" (I saved my file as "CC Math.docm)."
  2. Add four plain text content controls and the mathematical operators.
  3. Using the Content Controls Properties dialog, set the Title and Locking properties of the three variable content controls (A, B and C) as appropriate.
cc math 2
  1. Using the same dialog, set the Title and Locking properties for the sum content control.
cc math 3
  1. Using the Developer>Controls>Design Mode set the placeholder text for each of the content controls to "0."
cc math 4
  1. With the content control properties set and placeholder defined you are ready to add the necessary code to the document ContentControlOnExit event.
  2. Open the Visual Basic for Applications Editor (VBE) using Alt+F11. If not already showing, show the Project Explorer pane using Ctrl+r. In the Explorer, navigate to, expand and double-click the "ThisDocument" class module of the project.
cc math 5
  1. Paste the following code into the VBE code pane. The code includes:
VBA Script:
Option Explicit
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim oCC As ContentControl
'Handle OnExit event bug in Word 2007
  If Application.Version < "14.0" Then MAIN.SetDeveloperTabActive
  Select Case CC.Title
  Case "A", "B", "C"
'Validate/format data entered in variable content control.
    If Not IsNumeric(CC.Range.Text) Then
      Cancel = True
      Beep
      CC.Range.Select
      Exit Sub
    Else
      CC.Range.Text = FormatValue(CC.Range.Text)
    End If
'Update content control contents.
    Set oCC = ActiveDocument.SelectContentControlsByTitle("SUM=(A+B+C)").Item(1)
    With oCC
      .LockContents = False
      .Range.Text = FormatValue(MathAdd(ActiveDocument.SelectContentControlsByTitle("A").Item(1).Range.Text, _
      ActiveDocument.SelectContentControlsByTitle("B").Item(1).Range.Text, _
      ActiveDocument.SelectContentControlsByTitle("C").Item(1).Range.Text))
      .LockContents = True
    End With
  End Select
  Set oCC = Nothing
lbl_Exit:
  Exit Sub
End Sub

Function FormatValue(ByRef pStr As String) As String
'This function applies formatting to the numbers entered in the variable content controls _
 and to the results of the mathematical operation functions.
  If InStr(pStr, ".") > 0 Then
    FormatValue = Format(pStr, "##,####,####,##0.0###########;(##,####,####,##0.0###########);0")
  Else
    FormatValue = Format(pStr, "##,###,###,###;(##,###,###,###);0")
  End If
lbl_Exit:
  Exit Function
End Function

Function MathAdd(ByRef A As Double, B As Double, C As Double) As Double
'This is the mathematical operation function to compute the sum of the addition _
   content control variables. 
  MathAdd = A + B + C
lbl_Exit:
  Exit Function
End Function 

Site Note iconNotes:
     1. The code shown above includes a call to a separate procedure for handling the OnExit event bug in Word 2007. See: Content Controls. The code for this procedure is too long to publish here.  It is available in the demonstration document that you can download using the link at the bottom of this tips page.

     2. See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

Subtraction, multiplication and division

Similar steps and code can be used to perform mathematical operations involving content controls defined as variables for subtraction, multiplication and division. Examples are included in the demonstration document that you can download using the link at the bottom of this tips page.

cc math 6

Content controls can be placed in Word tables and used as both variables and for displaying the results of more advanced "Sum Left" or "Sum Above" calculations as illustrated below:

cc math 7

That's it! I hope you have found this tips page helpful and and informative. You can download a demonstration document for this tips page here: CC Math.

Share

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!

Click to donate British Pound Sterling                   Click to donate US dollars                   Click to donate EU euros

Search my site or the web using Google Search Engine

Google Search Logo