PDA

View Full Version : Adding Formfields



mgonzalez07
11-14-2005, 06:48 PM
Hello everyone. I am having trouble with this code and I am hoping someone can help. Here's the situation. I have two fill-in forms, the user fills in the first one and then runs a macro to automatically fill in the second form with the matching info. I have most of it working but what I want to do is write a macro that will add 4 entries in the first document, these being 4 numbers that are in 4 separate formfields, and place the result of the addition into 1 formfield in the second document. This is part of what I have for the normal 1 to 1 case.


DestDoc.FormFields("Name").Result = ActiveDocument.FormFields("Name2").Result
DestDoc.FormFields("Address").Result = ActiveDocument.FormFields("Address2").Result
DestDoc.FormFields("City").Result = ActiveDocument.FormFields("City2").Result
DestDoc.FormFields("State").Result = ActiveDocument.FormFields("State2").Result
DestDoc.FormFields("Zip").Result = ActiveDocument.FormFields("Zip2").Result



Can someone help with this.

fumei
11-16-2005, 12:38 AM
Please walk through this step-by-step.

I will write what I think you are saying...although you do not actually say it. Please DO say it.

You have a document. You take formfield results from that document and place them into formfields in another document.

DestDoc is taking the result of ActiveDocument.

This is poor programming. It is MUCH better to identify documents. You also do not state anything about opening sequences. If DestDoc is taking values from ActiveDoc, then DestDoc is NOT the active document...correct?

Use Document objects. Something like:

Dim FromDoc As Document
Dim ToDoc As Document
Set FromDoc = ActiveDocument
Documents.Open Filename:="c:\blah\destdoc.doc"
Set ToDoc = ActiveDocument
Now you can reference your source document as FromDoc. Not only that but you can declare a formfield object, and use that.
Dim FromFF As Formfields
Set FromFF = FromDoc.Formfields()
Set ToFF = ToDoc.Formfields()
ToFF("City2").Result = FromFF("City").Result
See my drift? OK. It sure seems that DestDoc has the first naming - eg. "City" and ActiveDocument has the SECOND naming - eg "City2". Then why is DestDoc taking the result? This is why clear naming may help.

Next. There is an issue of whether you are using the numbers as strings, or as numbers. Also, you do not state if the formfields are set a numeric formfields, or text formfields. Text formfields can be set to be numeric.

It really helps to think things out and post a very clear picture of what you have.

That being said. Here is an example of how you could do it.
Sub CountMe()
Dim i As Integer
Dim thisdoc As Document
Dim thisFF As FormFields

Set thisdoc = ActiveDocument
Set thisFF = thisdoc.FormFields
i = CInt(thisFF(1).Result) + CInt(thisFF(2).Result) + _
CInt(thisFF(3).Result) + CInt(thisFF(4).Result)
thisFF("Summed").Result = i
Set thisFF = Nothing
Set thisdoc = Nothing
End Sub
This makes a document object, a formfields object, adds up the first four formfields (using the index values of 1, 2, 3, 4), then places the sum into a final formfield "Summed".

At the end it destroys the objects. This is important. If you create objects in your code, destroy them (Set object = Nothing) as soon as you no longer are using them.