Copy Word Form fields to a different document

N

n_nally

Good Afternoon,

I need to copy some simple form fields to another open document that
has the same field names. So for example, I have a document that has
name, address and phone number form fields at the top. There is a
button that will open another Word document, then I want to copy the
contents of those form fields to the same form fields in the new
document. I am having trouble figuring out how to reference another
open document and then copy the text in the form fields from one to
the other. Please Help!

Nathan
 
G

Greg Maxey

Good Afternoon,

I need to copy some simple form fields to another open document that
has the same field names. So for example, I have a document that has
name, address and phone number form fields at the top. There is a
button that will open another Word document, then I want to copy the
contents of those form fields to the same form fields in the new
document. I am having trouble figuring out how to reference another
open document and then copy the text in the form fields from one to
the other. Please Help!

Nathan


Sub CopyFF()
Dim oFFMaster As FormFields
Dim oFFSlave As FormFields
Dim oFF As FormField
Set oFFMaster = ThisDocument.FormFields
Set oFFSlave = Documents.Add("C:\Test.dot").FormFields
For Each oFF In oFFMaster
oFFSlave(oFF.Name).Result = oFF.Result
Next
Set oFFMaster = Nothing
Set oFFSlave = Nothing
End Sub
 
G

GrPacker

Thanks Greg. I tried that out and was able to get it to work, but of
course I have a couple questions...

1) I use a document management system (Interwoven) so I cannot
reference a document stored on mapped drive because it is stored
differently. The document I need to copy into will already be open.
How do a reference a document that is already open?
2) How do I copy specific form fields from one document to another
instead of all form fields. I do not have all the same form fields in
the two documents nor do I want to copy all fields. How do I do this
manually for each form field?

Thanks again. You are a great help!
 
D

Doug Robbins - Word MVP on news.microsoft.com

Open the document that contains the data first (as the only document open),
then open the document into which you want the data inserted. Then run a
macro containing the following code:

Dim Source As Document
Dim Target As Document
Dim ff As FormField
Set Source = Application.Windows(1).Document
Set Target = Application.Windows(2).Document
For Each ff In Target.FormFields
ff.result = Source.FormFields(ff.Name).result
Next ff


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
G

Greg Maxey

Doug answered the first part. If you had lots of open documents then it
would probably be easier to display a userform listing the open documents to
choose from. You would then Set oFFSlave = to the selected file.

To copy only selected FormFields use something like this:

For Each oFF In oFFMaster
Select Case oFF.Name
Case Is = "Name", "Address"
oFFSlave(oFF.Name).Result = oFF.Result
Case Else
'Do nothing
End Select
Next
 
G

GrPacker

Thanks for the help guys, I will try all of that out.

So there is no way to just reference the actual name of an open Word
window?
 
G

Graham Mayor

Instead of eg
Application.Windows(1).Document change the '1' for the document path

eg

Dim Source As Document
Dim Target As Document
Dim ff As FormField
Set Source = Application.Windows("d:\Path\DocName1.doc").Document
Set Target = Application.Windows("d:\Path\DocName2.doc").Document
For Each ff In Target.FormFields
ff.Result = Source.FormFields(ff.name).Result
Next ff


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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