The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
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!
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.
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:
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
Notes:
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.
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.
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:
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.
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
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!