Auto add Name & date to a document save name

G

Guest

New here Hello
I am a complete novice so please be kind :)

I have created a template for a shift working handover log which will be
used twice a day. the log will be for a first shift and a back shift

When the Log is completed i want the document to be saved in the same way by
all users eg
01-10-05 firstshift.doc
or
01-10-05 backshift.doc

the templete that i have created has a drop down box to select the shift and
the date is added when the template is opened.

is it possible for this info to be automatically put into the file name for
saving ??

Hope this makes sense !

Thanx in advance

Phil G
 
J

Jay Freedman

The solution needs a couple of pieces of explanation.

- When a document is saved for the first time, if there's text in the
Title property (in the File > Properties dialog), that text is put
into the File Name box of the Save As dialog. The user could change it
there, but if they just hit the Save button that's what the file will
be named.

- You're going to need a macro in the template to put together the
date and the shift into a complete file name, and to stuff that into
the document's Title property.

- The macro, once it exists, can be chosen as the exit macro for the
dropdown. That means that when the user selects a shift and tabs or
clicks to the next field, the macro runs automatically. (To make this
work, the dropdown should be the first field in the form so that it's
selected when the new document opens. Otherwise the user could click
elsewhere and never enter the dropdown.)

- The code in the macro should be based on what's shown at
http://word.mvps.org/faqs/macrosvba/SetDefFilename.htm. You'll have to
modify it to take the value of the date (you didn't say where that is)
and the selection in the dropdown, which will be

Selection.FormFields(1).Result

and put those two strings together (using the & operator) to replace
the words "My title" that the example macro puts in the Title
property.

- To get started with a macro, see
http://www.word.mvps.org/FAQs/MacrosVBA/VBABasicsIn15Mins.htm and
http://www.gmayor.com/installing_macro.htm.

If you have trouble getting this working, post back. Include whatever
macro code you have so far, and explain what is or isn't working.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Guest

Thanx for the quick response Jay

Im a bit of out of my depth here, but i managed to follow your reply and
create a macro (my first ever) and link it to the drop down box to populate
the 'Properties - 'Title' Field so that when the document is saved the shift
selection is in the file name.

But i need to add the date the document is saved to the file name.

Within the Document template (shift Log) i have used 'Insert - Field -
Createdate' field within the log to display the date of the log followed by
the Shift selection dropdown box (this is where i have tied the macro to)

How do i tie the date to the file name also do i end up with the following
depending on the shift selection

01-01-05 First Shift.Doc
01-01-05 Back Shift.Doc
01-01-05 Weekend Shift.Doc

Thanks in advance

Phil G
 
J

Jay Freedman

Hi Phil,

The bit of code you need goes something like this (you can change to
the variable names or your choice):

Dim myCreateDate As String
Dim myFileName As String

myCreateDate = ActiveDocument _
.BuiltInDocumentProperties(wdPropertyTimeCreated)
myCreateDate = Format(myCreateDate, "MM-dd-yy")

myFileName = myCreateDate & " " & _
Selection.FormFields(1).Result

It works like this:

- The two Dim statements tell VBA that the variables are going to be
strings (series of characters). All variables should be declared as
some specific type (see
http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm).

- The next two lines are one long statement because the
space-and-underscore says "the next line is a continuation" (see
http://www.word.mvps.org/FAQs/MacrosVBA/_AtEndOfLine.htm). In that
statement, I tell VBA to look in the collection of built-in document
properties -- the ones in the File > Properties dialog -- grab the one
that holds the date and time the document was created, and put that
into the myCreateDate variable.

- The next line says to call the Format function (which is part of
VBA) and tell it to change the format of the myCreateDate string from
its original value (something like "5/20/2006 5:12:00 PM") to the
desired month-day-year numbers separated by hyphens. You could change
the order to day-month-year or the more easily sortable
year-month-day.

- The last two lines are again a single statement. It says to put
together the myCreateDate variable, a space, and the result of the
dropdown into one string, and store that in the myFileName variable.
You can then use the code you already have to store myFileName in the
Title property.

As an aside, there are always at least two ways to do anything in
Word. An alternative to this would be to set up the template with a
bookmark covering the CreateDate field in the text. Suppose the
bookmark was named bkCreate. Then the macro could use a statement like

myCreateDate = ActiveDocument.Bookmarks("bkCreate").Range.Text

instead of getting it from the built-in document property.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Guest

Hi Jay

im only just keeping up with you here :)

I have followed your reply and came up with the following code

Sub Test1()
'
' Test1 Macro
' Macro created 5/21/2006 by Phil
'
Dim myCreateDate As String
Dim myFileName As String

myCreateDate = ActiveDocument _
.BuiltInDocumentProperties(wdPropertyTimeCreated)
myCreateDate = Format(myCreateDate, "dd-MM-yy")

myFileName = myCreateDate & " " & _
Selection.FormFields(1).Result

With Dialogs(wdDialogFileSummaryInfo)
.Title = myFileName
.Execute
End With
End Sub

Which is again tied to the exit of the dropdown box, this populates the
Properties 'Title' field with exactly what i want the file to be called BUT
when the file is saved the file name is only the first part of the date eg
'21' instead of '21-05-06_Weekend'

Again thanks for the reples, im actually learning something here :)

Phil G
 
J

Jay Freedman

Hi Phil,

The macro looks right (except for a little funky indentation in the
With ... End With stuff -- I like to keep the matching statements at
the same indent, and with their contents indented further, because it
makes the blocks of code easier to read).

I don't understand where you're getting an underscore between the date
and the rest of the title, but that doesn't matter. The problem (as
mentioned in the "important gotcha" paragraph of
http://word.mvps.org/faqs/macrosvba/SetDefFilename.htm) is that if the
title contains any punctuation, Word ignores everything from the first
punctuation character onward when it makes the suggested Save As file
name. The only characters it allows are letters, numbers, and spaces.
I couldn't get Greg Chapman's workaround in that article to do
anything.

So... The answer seems to be that you'll have to settle for file names
like "21 05 06 Weekend.doc" if you want to have automatic generation
of the names. To do that, change the Format statement to use the
quoted expression

"dd MM yy"

instead of the one with hyphens.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Guest

I think i have achieved what i wanted

Thanks you very much for your help Jay it was most appreciated

I was hoping that all i had to do was tick a box, but obviously that would
have been too easy :)

Phil G
 

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