Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word: Summing Text Field Values 1

Status
Not open for further replies.

SilentAiche

Technical User
Dec 21, 2004
1,325
US
Hi Again,

I'm using Word 2002 on Win XP.

I have a large document saved as a form (sound familiar?).

I have several "stand alone" text fields in various places around the document that contain numeric values. I would like to be able to sum these at the end of the document.

I know how to use Shift-F9 and bookmark names to copy values from one field to another, but I can't figure out how to actually add two such fields together (i.e., Text1 + Text2 = (value in 3rd form field)).

Thanks in advance!
Tim

[blue]__________________________________________________
If you need immediate assistance, please raise your hand.
[/blue]
 
Hi Tim,
Are these the formfields? I am guessing they are with the names Text1, Text2.

This is fairly easy.

If Text1, Text2 and Text3 are set to be Numeric (a good idea), then use this as the OnExit macro for Text2. It just adds Text1 and Text2 together and puts the result in Text3.

Note: there is a better way.

Code:
Sub AddEm()
With ActiveDocument
    .FormFields("Text3").Result = _
        CInt(.FormFields("Text1").Result) + _
        CInt(.FormFields("Text2").Result)
End With
End Sub

Better way #1

Where you want the SUM, do not use another formfield. use a Field. Insert > Field. The top of the list is Formula. there should be a Formula button. Click it, and put in:

=SUM(Text1,Text2)

Press OK, and this field will display the results of text1 and Text2. Make SURE that Text1 and Text2 have Calculate on exit checked.


Better way #2

Where you want the SUM, do not use a formfield. Use a Bookmark. Use a similar Sub as the OnExit macro for Text2. This assumes a bookmark named text3SUM a a replacement for the formfield "Text3".

What it does:

Makes an variable = to SUM of Text1 and Text2
Goes to the bookmark
Inserts the SUM of Text1 and Text2 - this deletes the bokmark
moves selection range the length of the inserted SUM
creates a new bookmark with the same name

Code:
Sub AddEm2()
Dim i As Integer
ActiveDocument.Unprotect Password:=""
With ActiveDocument
i = CInt(.FormFields("Text1").Result) + _
        CInt(.FormFields("Text2").Result)
End With
With Selection
  .GoTo What:=wdGoToBookmark, Name:="text3SUM"
  .TypeText Text:=i
  .MoveStart Unit:=wdCharacter, _
    Count:=-Len(i)
  ActiveDocument.Bookmarks.Add _
     Name:="text3SUM", Range:=Selection.Range
  .Collapse direction:=wdCollapseEnd
End With
ActiveDocument.Protect wdAllowOnlyFormFields, _
    Password:=""
End Sub


Gerry
 
Gerry,

Better Way #1 is works perfectly (plus, it's easier for me to understand than code!). I knew there had to be a way. Have a star.

Thanks,
Tim

[blue]__________________________________________________
If you need immediate assistance, please raise your hand.
[/blue]
 
Thanks.

Fields are handy, and it will get your file size down as there is not as much overhead as having formfields taking the result.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top