Linking fields in Word forms

M

Mike

I have about 12 forms (separate Word files) which have common fields (e.g.
project number). Is there a way to populate all forms with the common data
when the first form is filled in?

Thanks
Mike
 
P

Peter Jamieson

It should, broadly speaking, be possible. Some rather poor code to get you
started - imagine you have c:\test\Doc1.doc open, have filled in a form,
then click a button connected to the CopyFormDataToAllDocs sub which should
copy values to doc2.doc and doc3.doc and leave them open.

You might of course need to consider any number of additional things, e.g.
a. error handling in general
b. dealing with different types of form field (this will cope with text
fields)
c. dealing with fields in different documents with the same names but
different types, or different lengths etc. etc.

Peter Jamieson

Sub CopyFormDataToAllDocs()
Call CopyFormDataToOneDoc(ActiveDocument, "c:\test\doc2.doc")
Call CopyFormDataToOneDoc(ActiveDocument, "c:\test\doc3.doc")
End Sub

Sub CopyFormDataToOneDoc(objSourceDocument As Word.Document, pathname As
String)
Dim objTargetDocument As Word.Document
Dim objSourceField As Word.FormField
Dim objTargetField As Word.FormField
Set objTargetDocument = Application.Documents.Open(pathname)
For Each objSourceField In objSourceDocument.FormFields
On Error GoTo NoSuchField
Set objTargetField = objTargetDocument.FormFields(objSourceField.Name)
MsgBox objSourceField.Name & ": " & objSourceField.Result
objTargetField.Result = objSourceField.Result
Set objTargetField = Nothing
NoSuchField:
Err.Clear
On Error GoTo 0
Next
Set objTargetDocument = Nothing
End Sub
 

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