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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding two text form fields in word 2

Status
Not open for further replies.
Hi Anthony,

Do you particularly want to do this in code? Why not just set the total field up as calculated, and set the underlying fields to "calculate on exit"?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 

Or, if you are doing the whole thing in code, why are you using formfields at all?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Well the form is what the users wanted.. and if it could be in code that would be great.

Just curious, what other ways is there of doing this.

Thanks for the response!
 
Agreed. But if you do need to do it by code:

1. are all your text formfield set as numeric? If you are using them that way, it is better to explicitly make them numeric. Just note that text entered will NOT give an error, it will return 0. So you will want to do error trapping for that.
Code:
Sub makeFFNumeric()
Dim aDoc As Document
Dim i As Integer
Dim aFF As FormField
i = 1
For Each aFF In aDoc.FormFields
    With aFF
        .Name = "Text" & i
        .Enabled = True
        .CalculateOnExit = True
        With .TextInput
            .EditType Type:=wdNumberText, _
                Default:="", Format:="0.00"
            .Width = 0
        End With
    End With
    i = i + 1
Next
Set aDoc = Nothing
End Sub

2. If you permanently want to set a field that will take the numeric values of two (or more) formfields, then:
Code:
Sub MakeASumField()

    Selection.Fields.Add Range:=Selection.Range, _
        Type:=wdFieldEmpty
    Selection.InsertFormula Formula:="=Text1 + Text2"
End Sub

You can of course go to whatever location needed to insert a sum field. And, as Tony pointed out, you must have the formfields set for Calculate on exit. The summing field will not work unless they are.

Other than that, we would need to know what your design involves. As for the "object required" - I went through it quickly, and it is probably because you are using "text1.result". All formfields have to be fully named. Maybe use a With statement?

Gerry
 
Hi Anthony,

Firstly, let me say I haven't studied the whole of your other thread so could have missed something.

The whole point of Word Forms is that Users fill them in. If you're providing document content via code you can do it without using Form Fields. You can do all your calculations in code and simply pop the results where you want them. FormFields seem like a real complication but, as I say, I haven't studied exxactly what you're doing.

Anyway, if you have them, set up the ones you want as totals as calculated and, when you're ready, do
Code:
ActiveDocument.Fields.Update

Alternatively, you could do something like
Code:
ActiveDocument.FormFields("Text3").Result = ActiveDocument.FormFields("Text1").Result + ActiveDocument.FormFields("Text2").Result

If neither of those works for you, come back.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
I changed my fields to numeric.

I tried this part of your code

Code:
Sub MakeASumField()

    Selection.Fields.Add Range:=Selection.Range, _
        Type:=wdFieldEmpty
    Selection.InsertFormula Formula:="=Text2 + Text6"
End Sub

text2 and text6 are the fields I wanted to add together

I added the sub went into my field text30 (where I wanted to display my sum) and on exit set it to your sub..

and I get an error

The command is not availiable

How can I go about incorporating your code to display in text30 (my sum field) - all behind my cal button.

I've tried using on exit macros on the form .. but the users seem some how to mess that up and not go into that field to update. .. thats why I'm trying to incorporating my code behind a button.

Thanks for both your help
 
Tony,

I think the first suggesting worked!

But the second one did not add the fields

Code:
ActiveDocument.FormFields("Text3").Result = ActiveDocument.FormFields("Text1").Result + ActiveDocument.FormFields("Text2").Result

What it did was..

ie.
Text1=12.98
Text2=1.58
Text3=12.981.58

I probably done something wrong but that's what I got. Just wanted to let you know.

(My Text1-3 field Type are Numeric. If that makes a difference.)

The first solution did work and I'm greatful for that!!

Thank you Tony and Gerry!

 
Sorry Anthony,

That's what happens when I type instead of testing first [smile].

The fields are TEXT. You need to convert them to NUMBERS. Try this (still untested!):
Code:
ActiveDocument.FormFields("Text3").Result = [red]CDbl([/red]ActiveDocument.FormFields("Text1").Result[red])[/red] + [red]CDbl([/red]ActiveDocument.FormFields("Text2").Result[red])[/red]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
OK, you have design problems, and seem to mixing different processes. Please note the red emphasis

1. the code:
Code:
Selection.Fields.[COLOR=red]Add[/color red] Range:=Selection.Range, _
        Type:=wd[COLOR=red]FieldEmpty[/color red]
Selection.InsertFormula Formula:="=Text2 + Text6"

makes a new Field that will total formfield "Text2" and "Text6". This field is NOT a formfield. Plus it will only do that total if Text2 and Text6 have calculate on exit checked.

here is something that should work. You say you are using a button. I am assuming it is a ActiveX control button IN the document. You also mention Text30, but I am unsure if you want Text2 + Text6 = Text30, but that is what this does.

Code:
Sub cmdCalc_Click()
Dim aDoc As Document
Set aDoc = ActiveDocument
aDoc.FormFields("Text30").Result = _
    CInt(aDoc.FormFields("Text2").Result) + _
    CInt(aDoc.FormFields("Text30").Result)
Set aDoc = Nothing
End Sub

It simply makes the .Result into integers that can be added.



Gerry
 
Thanks for the responses..

I'll give them a try tommorrow

Thanks! this is great information!
 
Ooops that thrid result item should be text6, not text30...

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top