1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| 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 "Taillecm", "Poidskg"
'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("BMI").Item(1)
With oCC
.LockContents = False
.Range.Text = FormatValue(MathAdd(ActiveDocument.SelectContentControlsByTitle("Taillecm").Item(1).Range.Text, _
ActiveDocument.SelectContentControlsByTitle("Poidskg").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 = (Poidskg / ((Taillecm / 100) * (Taillecm / 100)))
lbl_Exit:
Exit Function
End Function |
Partager