PDA

View Full Version : Calculating dynamic fields of another table



lbb4ever
04-22-2009, 12:36 PM
Hello,I have created a protected form document. This document has several tables. One of them, specifically, was designed to generate new rows, complete with formfields, when the user clicks a macrobutton. So this table can have one row (sans the header row) or 20, if they so desire. I need to have another field outside of this table which totals one of the said table's columns. I am not sure how to reference this field to "autosum" this particular column of this particular table.Any help would be sooooooooooo appreciated :)Thanks in advance!

lbb4ever
04-22-2009, 01:22 PM
I think I found a solution. Works like a charm.

Sub runSum()
ActiveDocument.Unprotect
ActiveDocument.Tables(10).Range.Columns(4).Select
ActiveDocument.FormFields("bName").Result = Selection.Calculate
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

fumei
04-22-2009, 01:48 PM
"I need to have another field outside of this table which totals one of the said table's columns. I am not sure how to reference this field to "autosum" this particular column of this particular table."

This is not a good function of Word. This is what Excel is for.

However, it can be done.

If the Formula field - which is what you are talking about - is doing a SUM of a table it is not in, you need to bookmark the table. Actually bookmarking tables is a good idea a general practice.

{ =SUM(Table2 C:C) }

would be a formula Field which sums all the cells in the "C" - or column3 - of the table bookmarked as "Table2".

You may want to look in Word Help. Try putting:

Field codes: = (Formula) field

into Help. There are examples.

Note that you will have to Update the field if you ever change the number of rows in that table. One good thing though, because the table is bookmarked, you do NOT have to change the formula. The formula will work whether the table has 3 rows, or 30 rows. but again, if this changes the field RESULT is not automatically updated. You must update it.

That being said, if you are printing, then simply check "Update fields" as a Print Option.

fumei
04-22-2009, 01:58 PM
Ah, you did not state you wanted to have the result in another formfield. This is why is always helpful to be explicit.

"I need to have another field outside of this table which totals one of the said table's columns. I am not sure how to reference this field to "autosum" this particular column of this particular table."

My bolding.

You meant another formfield.

What, BTW, is going to fire Sub runSum? Your solution will indeed work, although it uses Selection. If you use a Field (not a formfield) like my example above, then you could use:
Sub runSum()
With ActiveDocument
.Unprotect
.Fields.Update
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub
which updates the Field without selecting anything.

fumei
04-22-2009, 02:02 PM
BTW: why are putting the resulting sum into a formfield?? Formfields are for user INPUT. In this case, you are not getting user input, YOU (as the developer) are calculating a value.

Further, once you have in fact put the sum into bName.Result, there is absolutely nothing to prevent your calculated sum:

113

being changed into "blag badooba". The formfield is STILL open for user input.