Automatically creating file names for dicuments based on data ente

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to automatically created a file name to save a file created
from a template based upon the data entered in to the fields?
 
Automatically?
Fields?
Word version?

It is certainly possible to save documents with a filename based on
information in the document, but you need to tell us something about the
document.

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Sorry not enough inf.
Im running word 2007 and have created a template. Ive got some feild codes
in a template that include the following: Project, version number, title,
document category (my own categories for different type of documents used in
our company ie SOP, Finished Goods Specification, Record...). Currently
staff have to duplicate some information in the file name as well as put it
into the cover page of the document.

The file name that we use is the sum of the above fields all in a particular
order. For example the file name I want people to consistently use is:

"Project_Name, Document_Title, Document_Type, Version_#".

Is there a way that Word can automate the name of the file based on data
entered into the data fields?

Thanks
 
Still not enough info. We cannot see your documents!
What kind of 'field codes' are these? How are they inserted into the
document and how are they stored?

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Hi thanks for persisting with me.
The field codes that I am using are from a word 2003 template I made are
"text Form Field Codes". I'm currently got word 2007 on my computer.

I have inserted the text fields into a table that is on the cover page of
the template. Currently the template is stored on my C drive and the
documents made from the template are in another folder (although that can
change I suppose if it needs too).

Hope I have answered your questions with enough detail.

cheers
 
Assuming your formfields bookmark names are those you indicate, then the
following macro run on exit from the last of the relevant fields will save
the document in the format you require in the current folder. Alternatively
you can add the code to a toolbar save button,

Sub FileSaveForm()
Dim sValue1 As String
Dim sValue2 As String
Dim sValue3 As String
Dim sValue4 As String
Dim sFname As String

With ActiveDocument
sValue1 = .FormFields("Project_Name").Result
sValue2 = .FormFields("Document_Title").Result
sValue3 = .FormFields("Document_Type").Result
sValue4 = .FormFields("Version").Result
sFname = sValue1 & Chr(44) & sValue2 & Chr(44) _
& sValue3 & Chr(44) & "Version " & sValue4 & ".doc"
If InStr(1, sFname, " ") <> 0 Then
sFname = Replace(sFname, " ", "_")
End If
MsgBox sFname
.SaveAs sFname
End With
End Sub


http://www.gmayor.com/installing_macro.htm

Note the macro does not check whether the filename already exists!


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
You can remove the line MsgBox sFname - that was simply for testing.

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
thanks very much
--
agibson


Graham Mayor said:
You can remove the line MsgBox sFname - that was simply for testing.

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
You are welcome :)

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Hi GM, its taken me a while to learn macros to use the code that you gave me
a month ago but I just got it to work.

its fantastic

thanks
 
You should have said that you were having problems?
Glad you got there in the end. :)

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Hello again,

If I could trouble you for a question please.
I've got the macro running as I previously have said. I've even added an
extra Value, and its running great. However all the values are in the tile
are only separated by a comma - no space between them. It makes it kinda
hard to read. See below

C:\Users\Andrew\Documents\D_1234,Cool_QMS,test_document,SOP,V_1.00.doc

Is there a way to put a space between each sValue or some other type of
charater. I tried " " on the off chance that it might work but got an error.

C:\Users\Andrew\Documents\D_1234, Cool_QMS, test_document, SOP, V_1.00.doc
 
The macro was originally written to remove spaces and replace them with
underscores using the section

If InStr(1, sFname, " ") <> 0 Then
sFname = Replace(sFname, " ", "_")
End If

and I had deliberatly refrained from adding spaces after the commas as I
assumed from your original post that you didn't want them. So you can start
by losing that bit.

Next the commas are inserted with & Chr(44) &. If you want a space after the
comma you could simply extend each occurrence to

& Chr(44) & CHR(32) &

or

& Chr(44) & " " &

but if you leave

If InStr(1, sFname, " ") <> 0 Then
sFname = Replace(sFname, " ", "_")
End If

those spaces will be replaced with underscores. If you want the underscores
in the field results, you can add them
to the individual field results eg

sValue1 = .FormFields("Project_Name").Result
If InStr(1, sValue1, " ") <> 0 Then
sValue1 = Replace(sValue1, " ", "_")
End If
sValue2 = .FormFields("Document_Title").Result
If InStr(1, sValue2, " ") <> 0 Then
sValue2 = Replace(sValue2, " ", "_")
End If
sValue3 = .FormFields("Document_Type").Result
If InStr(1, sValue3, " ") <> 0 Then
sValue3 = Replace(sValue3, " ", "_")
End If
sValue4 = .FormFields("Version").Result
If InStr(1, sValue4, " ") <> 0 Then
sValue4 = Replace(sValue4, " ", "_")
End If

If you find it simpler to manipulate you can take this a step further and
add the commas and spaces to each string before assembling them in the
sFname string eg

With ActiveDocument

sValue1 = .FormFields("Project_Name").Result
If InStr(1, sValue1, " ") <> 0 Then
'Replace the user entered space with an underscore
sValue1 = Replace(sValue1, " ", "_")
'add the comma and space
sValue1 = sValue1 & Chr(44) & Chr(32)
End If

sValue2 = .FormFields("Document_Title").Result
If InStr(1, sValue2, " ") <> 0 Then
sValue2 = Replace(sValue2, " ", "_")
sValue2 = sValue2 & Chr(44) & Chr(32)
End If

sValue3 = .FormFields("Document_Type").Result
If InStr(1, sValue3, " ") <> 0 Then
sValue3 = Replace(sValue3, " ", "_")
sValue3 = sValue3 & Chr(44) & Chr(32)
End If

sValue4 = .FormFields("Version").Result
If InStr(1, sValue4, " ") <> 0 Then
sValue4 = Replace(sValue4, " ", "_")
sValue4 = sValue4 & Chr(44) & Chr(32)
End If

sFname = sValue1 & sValue2 _
& sValue3 & "Version " & sValue4 & ".doc"
.SaveAs sFname
End With

and if you don't want underscores at all it is even simpler:

With ActiveDocument
sValue1 = .FormFields("Project_Name").Result
sValue1 = sValue1 & Chr(44) & Chr(32)

sValue2 = .FormFields("Document_Title").Result
sValue2 = sValue2 & Chr(44) & Chr(32)

sValue3 = .FormFields("Document_Type").Result
sValue3 = sValue3 & Chr(44) & Chr(32)

sValue4 = .FormFields("Version").Result
sValue4 = sValue4 & Chr(44) & Chr(32)

sFname = sValue1 & sValue2 _
& sValue3 & "Version " & sValue4 & ".doc"
.SaveAs sFname
End With
End Sub

Does that help?


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
I've been using this macro for some time and its working fine. however with
word 2007 and the excellent "properties" section I think it would be better
to use these fields instead of "form fields".

Is this a simple change?

thanks
 
Word has always had the properties section, however it was you who indicated
that you wanted to extract the information from form fields, after much
pressing on the topic. If you want to extract the data from the document
property fields then that too is possible, but inserting the data into the
doc property fields in the first place is less convenient than using form
field data that is already in your document, when you want an automated
process.


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

My web site www.gmayor.com

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

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

Back
Top