Copying FormFields From One Doc to Three Other Docs: Word 2007

C

Cole

Word 2007; We've got multiple forms that have the same FormFields
(FF). I'm working on a script to:

1) Read the value in one FF = "ClearQuest_ID".
2) Save the document with that value as part of the name.
3) Open a Template document "Target1" as Document 1.docm (for
instance.)
4) Save Target1 as a ".docm" to a new folder (i.e. the project folder
with the same ClearQuest_ID as part of its name.
5) Copy the four FF's in the source document into the Target1
document.
6) Save the Target1 document.
7) There will actually be three target documents to copy the FF's to
when all is said and done.

I have not had opportunity to work with VBA for a couple of years now,
and am a novice. But I scalped some code online to throw the code
below together as a starting point. This should at least serve as
pseudocode for what I am trying to achieve.

Initial issues I can see:

A) For some reason, when I put a button on the form and double click
on it to see the VBA code, it ends up under the Project in
"ThisDocument" instead of the Module that I inserted. Don't know how
to get the button to be in Module1, as I believe it is supposed to
be. I want the button to perform steps 1-6 above.
B) When I populate the value of a FF in the Source form, then the FF
seems to disappear as a FF for the code to find. (I created the FF
via Developer / Controls / Legacy Tools / Text Form Field. I named
the FF via Controls / Properties and put the name into the Bookmark
field. This seems to be working since the FF are found by the code
but have a null value. When I put a value in, the FF is then not
found.)
C) I'm sure any expert will find other issues with my code - if you
care to advise.

Any help would be greatly appreciated!
Cole

Private Sub CommandButtonSave_Click()

Dim strDirPath, strNewSourceName, strNewFolder
Dim Source As Document
Dim oFF As FormField
Dim Test
strDirPath = "\\NT01234D\CCBDOCS:\BBX\"
Set Source = ActiveDocument
strNewSourceName = "MySourceDoc.docm"

Test = Source.FormFields.Count

'Find the new folder name:
For Each oFF In Source.FormFields
If oFF.Name = "ClearQuest_ID" Then
strNewFolder = Source.FormFields(oFF.Name).Result
End If
Next oFF

' Open folder - if does not exist, create it:
Dim myfolder As String
myfolder = "\\NT01234D\CCBDOCS:\BBX\" & strNewFolder
If Len(Dir(myfolder)) = 0 Then
Dim fso, fldr
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set fldr = fso.CreateFolder(myfolder)
End If

'Save the Document:
ActiveDocument.SaveAs (myfolder & "Mag-E.docm")

'Open Target1 Template doc and SaveAs to project folder:

Dim Target1 As Document
Set Target1 = Application.Windows(strDirPath & "Testing
Approvals.dotm").Document
Target1.SaveAs (myfolder & "Testing Approvals.docm")

'Copy the FormFields From Source (This Doc) to Target1:

Dim Source As Document
Dim Target As Document
Dim ff As FormField
Set Source = Application.Windows(myfolder & "Mag-E.docm").Document
Set Target = Application.Windows(myfolder & "Testing
Approvals.docm").Document
For Each ff In Target.FormFields
ff.Result = Source.FormFields(ff.Name).Result
Next ff

'Next do the same for Target2 & Target3:
'Etc.

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