So it's a form? You don't need a macro to achieve this, you can do it with a
conditional field construction - in this case
{ IF { =({ Val1 } + { Val2 } + { Val3 } + { Val4 } + { Val5 } + { Val6 } +
{ Val7 } + { Val8 }) / 8 } <> "!Syn*" "{ =({ Val1 } + { Val2 } + { Val3 }
+ { Val4 } + { Val5 } + { Val6 } + { Val7 } + { Val8 }) / 8 \# 0}"
"Selection includes an empty field"}
will round the result to a whole number. Change the switch \# 0 as
required -
http://www.gmayor.com/formatting_word_fields.htm Check the
calculate on exit check box of at least the last field that contributes to
the calculation.
Or
You could use a calculated form field
Or
You could use a macro (if you can persuade remote users to run the macro
against their better judgement). The following is based on your code, but
writes the result in the bookmark not against it, thus not adding to the
result should the macro be run again. You could instead write the result to
another form field and the form does not require unlocking to achieve that.
..FormFields("Average").Result = vTot
The macro can be run on exit from the last formfield that contributes to the
calculation or from a toolbar button..
Sub GetAvg()
Dim ff As FormField
Dim ad As Document
Dim rAv As Range
Dim v1, v2, v3, v4, v5, v6, v7, v8 As Integer
Dim vTot As Double
Set ad = ActiveDocument
vTot = 0
With ad
v1 = Val(.FormFields("Val1").Result)
v2 = Val(.FormFields("Val2").Result)
v3 = Val(.FormFields("Val3").Result)
v4 = Val(.FormFields("Val4").Result)
v5 = Val(.FormFields("Val5").Result)
v6 = Val(.FormFields("Val6").Result)
v7 = Val(.FormFields("Val7").Result)
v8 = Val(.FormFields("Val8").Result)
'Get total and divide by 8
vTot = (v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8) / 8
'unlock form real fast
.Unprotect
Set rAv = ad.Bookmarks("txtOverall").Range
rAv = vTot
.Bookmarks.Add ("txtOverall"), rAv
.Protect wdAllowOnlyFormFields, NoReset:=True, Password:=""
End With
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>