2
votes

I need to multiply the values of two text boxes and display the sum in a third text box in Word.

I have tried every variation of code I can think of.

When entering data in the first two text boxes, no error is generated but no answer appears in the third text box.

I am trying to multiply the results entered into Word text boxes (Legacy Forms, Text Form Fields) bookmarked "Text61" and "Amount247", showing the results in the same type of text box, bookmarked "Text71".

Sub MultiplyTotal()
Dim ff As String
ff = ActiveDocument.FormFields("Text61").Result
ff = ActiveDocument.FormFields("Amount247").Result
ff = ActiveDocument.FormFields("Text71").Result = ("Text61") * ("Amount247")
End Sub
1

1 Answers

0
votes

The code in the question is on the right track for performing the calculation, it just needs to use the variable(s) for capturing the results and performing the calculation a bit differently.

One variable is required for each item to be included in the calculation.

Since calculations are done with numbers, it's also helpful to force-convert the text (String) to a numerical value (the Val function), although if the user is careful it's not strictly necessary in VBA - but a good habit.

Sub CalculateFF()
    Dim ff1 As String, ff2 As String
    ff1 = ActiveDocument.FormFields("Text61").Result
    ff2 = ActiveDocument.FormFields("Amount247").Result
    ActiveDocument.FormFields("Text71").Result = Val(ff1) * Val(ff2)
End Sub

Note that it's not strictly necessary to use code to perform simple calculations in Word documents containing form fields, where forms protection is activated. A calculation type of form field can be used that executes a calculation without the need of code. If that approach is of interest, ask in an end-user venue such as Super User.