I have a Word document with a table that contains the price of an item, the order amount and the item sum, where amount and item sum are Legacy Form Fields:
There are other items available as options that you can chose via checkbox (also Legacy Form Fields):
Code
Now I've created a function that calculates the item sum
Sub order_amount_Click()
Dim amount As Integer
Dim valueFromCell As String
Dim list
Dim value As Double
amount = ActiveDocument.FormFields("order_amount").Result
If amount > 0 Then
valueFromCell = ThisDocument.Tables(1).Cell(5, 3).Range.Text
list = Split(valueFromCell, " ")
value = list(0)
subsum = value * amount
ActiveDocument.FormFields("order_amount_sum").Result = subsum
Else
ActiveDocument.FormFields("order_amount_sum").Result = 0
End If
CalculateSum
End Sub
and another function that calculates the item sum for additional items (if the checkbox is activated) multiplied by the order amount:
Sub optional_item_Click()
Dim checkbox As Integer
Dim valueFromCell As String
Dim list
Dim value As Double
Dim amount As Integer
Dim subsum As Double
checkbox = ActiveDocument.FormFields("optional_item1").Result
amount = ActiveDocument.FormFields("order_amount").Result
If checkbox = 1 And amount > 0 Then
valueFromCell = ThisDocument.Tables(1).Cell(7, 3).Range.Text
list = Split(valueFromCell, " ")
value = list(0)
subsum = value * amount
ActiveDocument.FormFields("optional_item1_sum").Result = subsum
Else
ActiveDocument.FormFields("optional_item1_sum").Result = 0
End If
CalculateSum
End Sub
(There are as many Click()
functions as there are optional_item
fields, 17 in sum - I haven't generalized the function yet.)
The last line within both Sub
s is a function call, that calculates the net sum, the VAT and the final total sum
Function CalculateSum()
Dim net As Double
Dim vat As Double
Dim total_sum As Double
net = CDbl(ActiveDocument.FormFields("order_amount_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item1_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item2_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item3_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item4_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item5_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item6_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item7_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item8_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item9_sum").Result)
' Cannot compile when line too long, so splitting into two statements
net = net + CDbl(ActiveDocument.FormFields("optional_item10_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item11_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item12_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item13_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item14_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item15_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item16_sum").Result) + CDbl(ActiveDocument.FormFields("optional_item17_sum").Result)
'MsgBox "net " & net
ActiveDocument.FormFields("net_sum").Result = net
vat = net * 0.19
'MsgBox "vat " & vat
ActiveDocument.FormFields("vat_sum").Result = vat
total_sum = net + vat
'MsgBox "total " & total_sum
ActiveDocument.FormFields("total_sum").Result = total_sum
End Function
Problem
The code itself works fine, all elements are calculated correctly. But there are two major problems that make the whole document almost unusable (all actions for users are restricted to enter their name on top of the document, chose the amount and de-/activate the checkboxes; sum fields aren't accessible):
- When I perform an action (e.g. enter an amount) the
CalculateSum()
function visibly loops over allsum
fields (spanned over three pages) and weirdly scrolls on the document along thesum
fields. - When I click on a checkbox on the 2nd or 3rd page, the document scrolls up to the first page to the place where I can enter the amount of pieces I want to order.
Now how can I supress all that looping over the sum
fields and weirdly scrolling around? And how can I prevent the document from scrolling up to the 1st page?
Any help appreciated! (I'm also thankful for side comments on my code, as I'm new to VBA.)
Update #1
I've added a screencast showing the error.
Update #2
The basic problem seems to be that the macro is scrolling to the position I am referring to in my script, e.g. if I'm using
ActiveDocument.FormFields("total_sum").Result = total_sum
it's scrolling to the total_sum
field.