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

Word VBA automatic file naming

Status
Not open for further replies.

shaleen7

MIS
Jun 23, 2002
188
US
Is it possible to have a word document automatically save a certain filename based on specified fields in a template?
The word template called Employeeinfo.dot reads:

Employee Information Template
The employee fills out the information in the template.

First Name field: Terri
Last Name field: Smith
Dept Field: Accounting
Location Field: Princeton
Then the employee saves the document.

The word template would save as c:\ Terri Smith - Accounting - Princeton.doc


I tried this code. But it's not working. Part of the code is in red when I placed it in word vb. I think I missed a step.

Dim strFilename As String
strFilename =ActiveDocument.FormFields(1).Result & " " &
ActiveDocument.FormFields(2).Result _
& " " & ActiveDocument.FormFields(3).Result & " " &
ActiveDocument.FormFields(4).Result
ActiveDocument.SaveAs "c:\" & strFilename


Please explain slowly. Thanks for the help in advance.
 
It looks like you missed the .doc so you can change it to:

ActiveDocument.SaveAs "c:\" & strFilename & ".doc"

[Afro2]
 
Thanks. I tried adding the .doc but it still doesn't work. I'm still see part of the code is in red. Everything is in red except the line that reads ActiveDocument.FormFields(4).Result


Public Sub test()

Dim strFilename As String
strFilename = ActiveDocument.FormFields(1).Result & " " &
ActiveDocument.FormFields(2).Result & " " &
ActiveDocument.FormFields(3).Result & " " &
ActiveDocument.FormFields(4).Result
ActiveDocument.SaveAs c:\ &"strFilename"&.doc"
End Sub

Here's what I did. I created a template called Employee info. I inserted four text form fields and then I inserted the code in a sub procedure. I then placed employee data into the form fields and saved the document. But nothing happened. It didn't use the field info as the file name. Help?
 

If you have the code exactly as it is reproduced then the reason most of it is in red (which means it is so much in error VBA can't do anything with it) is because you do not have continuation marks so VBA thinks each line is a separate statement and cannot make sense of it that way. Try ...

strFilename = ActiveDocument.FormFields(1).Result & " " & _
ActiveDocument.FormFields(2).Result & " " & _
ActiveDocument.FormFields(3).Result & " " & _
ActiveDocument.FormFields(4).Result

You also have your quotes on the SaveAs in all the wrong places which would explain why that line is red.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top