Adding values in formfields

J

Josh

I am trying to create a function that will add the values of 2 text boxes
based on if the checkbox is true or false. I am using Word 2003 but it needs
to work on Word 2000 also.
If the checkbox is true then I want the value of Copy divided by Pages
If False then I want the value of Copy times the value of Sheets.

Currently: Copy, Pages, and Total are all textbox form fields
If CheckBox1.Value = True Then
ActiveDocument.FormFields("Total").Result = ActiveDocument.FormFields
("Copy" / "Pages")
Else
ActiveDocument.FormFields("Total").Result =
ActiveDocument.FormFields("Copy" * "sheets")
Endif

Can this be done somehow? Would I have to use something other than
textboxes? The users needs to be able to update the formfields with their
data. Thanks for looking.

Josh
 
J

Jay Freedman

Hi Josh,

You need to go through the whole series of object.property stuff for
*each* form field. You can shorten the agony a bit by using the With
statement:

If CheckBox1.Value = True Then
With ActiveDocument
.FormFields("Total").Result = _
.FormFields("Copy").Result / .FormFields("Pages").Result
End With
Else
With ActiveDocument
.FormFields("Total").Result = _
.FormFields("Copy").Result * .FormFields("Sheets").Result
End With
End If

(Notice the use of space+underscore as a line-continuation character.
See http://www.word.mvps.org/FAQs/MacrosVBA/_AtEndOfLine.htm.)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
T

Tony Strazzeri

Hi Josh,

Not sure what you are trying to do here because you appear to be mixing
Document form fields and UserForm (VBA) fields/controls.
From this: ActiveDocument.FormFields("Total")
I think you are referring to document form fields
From this: CheckBox1.Value
I think you are referring to Usefrorm controls.

But youcould have a mixture of both (sortof) in the document.

Can you tell us what you want to accomplish by this or at least what
fields you are actually using?

Do the come from the "Form" toolbar or from the "Control Toolbox"
toolbar or are meant to all be in a Userform?


Cheers
TonyS.

le to update the formfields with their
 
T

Tony Strazzeri

Not sure how much experience you have with Word controls etc.

Thinking further, I played around with the "Control Toolbox" controls
since I have not used these for a long time (I generally avoid document
form fields and these controls in documents in preference for doing
everything in VBA UserForms.

In case you do not have much experience try the following example using
just the "Control ToolBox" toolbar.

Drop a CheckBox and four TextBoxes from that toolbar into a document.
Right click each one and select properties, then name the CheckBox
"CheckBox1" and the textbox controls "Copy", "Pages", "Sheets" and
"TotaL" accordingly.

Right click on any of the controls and select "View Code". This will
drop you into the VBA IDE in the "ThisDocument" contsainer for the
docuent you are using. You should see two lines of code as shown
below. Where the ??? will vary depending on the control you used.

Private Sub ??? ()
End Sub

Select both of these lines and replace them with this code
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Total = Copy / Pages
Else
Total = Copy * Sheets
End If
End Sub

Now go back to the document and exit "Design Mode" by clicking the
first button on that toolbar. You'll know if you are in or out of
design mode by how the controls in your document behave when you click
on them. In design mode they will have 'handles' around them, whereas
out of design mode the cursor will be inside the control allowing you
to enter values.

Now put some values into the appropriate fields and then click on the
checkbox.

Does this help?

Cheers
TonyS,
 
J

Josh

Thanks, this is exactly what I needed for this. Hopefully I will be able to
use this for bigger projects in the future.
 
T

Tony Strazzeri

OK, I didn't expect it would be quite what you wanted because I am not
really sure what you are trying to do.

Do you need more help?
How does it differ from what you wanted?
TonyS.
 

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