Beginner: Calculating with fields

  • Thread starter Matthias Becker
  • Start date
M

Matthias Becker

Hello

i am a beginner with Word-VBA and have a question.
For a formular i have to use multiple Textfields. These are called quant1,
quant2, ... quant15 and singelPrice1 to 15. I want to calculate the total price
into a third field which also shall be included in the Total field.

My question is now, how to do so?

I tried to set the fieldproperties to calculation but i do not get the right
result (always 0,00). Then i tried it with vba but i cannot acces any of the
fields.

I am useing Word 2000.

Thank you
Matt
 
D

DeeJee

For a formular i have to use multiple Textfields. These are called quant1,
quant2, ... quant15 and singelPrice1 to 15. I want to calculate the total price
into a third field which also shall be included in the Total field.
My question is now, how to do so?
I tried to set the fieldproperties to calculation but i do not get the right
result (always 0,00). Then i tried it with vba but i cannot acces any of the
fields.

Simple example with 4 textfields. Text1, Text2, Text3, Text4
Set fieldprop to number.
Set Tmp to what you need, maybe Currency, but don't use Variant

Sub Test()
Dim Tmp As Long 'You have to set this to what you really need!!
Tmp = ActiveDocument.FormFields("Text1").Result
Tmp = Tmp + ActiveDocument.FormFields("Text2").Result
Tmp = Tmp + ActiveDocument.FormFields("Text3").Result
ActiveDocument.FormFields("Text4").Result = Tmp
End Sub
 
D

Doug Robbins - Word MVP

Hi Matt,

In the third field for each item, insert the formula

= quant1 * singelPrice1

etc.

In the properties dialog for each quant and singelPrice, check the Calculate
on Exit box

for the total price you must use the following formula

= quant1 * singelPrice1 + quant2 * singelPrice2 + ..... + quant15 *
singelPrice15

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
H

Harold Kless[MSFT}

Hi Matthias
You do a calculation using FormFields without code or you can write a macro
that will fire on Exit from a formfield.
1. without using a macro.
Set the properties of the formfields Quant2 thru Quant15 to be numbers. Set
the property of Formfield of the Total field to be a Calculation. in the
Expression box add a formula =sum(quant1,quant2,quant3.....quant15)
In the properties of Quant15 check the box to Calculate on Exit.

2. If you wish to use a macro, this is a sample that can work. In the
properties of the Total field set the AddFieldValues for the Run Macro on
Entry:
Sub AddFieldValues()
Dim myField As FormField
Dim mySum As Integer
For Each myField In ActiveDocument.FormFields
If UCase(Left(myField.Name, 5)) = "QUANT" Then
mySum = mySum + myField.Result
End If
Next
ActiveDocument.FormFields("Total").Result = mySum

End Sub

I would suggest using the option #1 for simplicity and avoid potential
errors.

Harold Kless, MCSD
Support Professional
Microsoft Technical Support for Business Applications
(e-mail address removed)

--


This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
M

Matthias Becker

Hello

i know choosed to interate woth a "for i=1 to 15"-loop trough the fields. But
every time i switch the fields with the Tabulator the fields begin to flicker a
calculation takes about 1 to 2 seconds.

Is there a better way?

Thank you
Mtt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top