Automatic Path with Date

C

codeweb

I have some questions hopefully someone can assist.

I want to save my word documents by default in the d:\path location
with the following format

"2010-02-26 File Name.doc "– I would like to insert the date
automatically.

Appreciate your assistance.
 
D

Doug Robbins - Word MVP

Use a macro containing the following commands:

With Dialogs(wdDialogFileSaveAs)
If .Display Then
With ActiveDocument
.SaveAs Format(Date, "yyyy-mm-dd") & " " & .Name
End With
End If
End With

If you create the macro with the name of Sub FileSaveAs() in your normal.dot
template or in an add-in, it will run whenever the File>SaveAs command is
used.

--
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
 
J

janey

I was very interested in this reply as I save documents in this way so it
will save me having to put the date in each time I create a document.

However, is there a way of NOT running this macro - e.g. if I am creating a
template which does not need to have the date in the name?
 
G

Graham Mayor

Instead of naming it FileSaveAs() call it something else and run it from a
toolbar button or suitable keyboard shortcut.
or
Insert the FileSaveAs macro only in the template that requires it - and not
the normal template.

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Doug Robbins - Word MVP

Replace the line

.SaveAs Format(Date, "yyyy-mm-dd") & " " & .Name

with

.SaveAs .Name & " " & Format(Date, "yyyy-mm-dd")

--
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
 
J

janey

I think I must be missing something!

I want to use this macro in my office templates only.
I started with just one of them e.g. Office1.dot (which I brought over from
Word 2003 to 2007).
I created a macro called FileSaveAs:

Sub FileSaveAs()
'
' FileSaveAs Macro
' Saves a copy of the document in a separate file
'
With Dialogs(wdDialogFileSaveAs)
If .Display Then
With ActiveDocument
.saveas .Name & " " & Format(Date, "yyyy-mm-dd")
End With
End If
End With
End Sub

I then closed the template and opened a new document based on it. When I
pressed Save and put in a file name, e.g. Test, I got a file called
"Document1 2010-02-28.docx

How do I get this to be: Test 2010-02-28.docx please?

When I have this right, do I have to put the same macro in my other office
templates individually?
 
D

Doug Robbins - Word MVP

Use:

Dim Fkill As String
Dialogs(wdDialogFileSaveAs).Show
With ActiveDocument
Fkill = .FullName
.SaveAs .Name & " " & Format(Date, "yyyy-mm-dd")
End With
Kill Fkill


--
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

Graham Mayor

Hmmm! I am not sure how that takes us forward here. As I see it, the problem
is in the use of ActiveDocument.Name to rename the document. If the document
has not been saved this will result in
Document1 2010-03-01.docx
If the document has already been saved with that name the macro will save
with the name
Document1 2010-03-01.docx 2010-03-01.docx
You need to test whether the document has been saved and then if not prompt
for the part of the filename before the date. If then the document has been
saved, you could eliminate the date from the filename and add a new name.
This of course will delete any document of the same name saved on the same
date.
There seems little point showing the Dialog(wdDialogFileSaveAs) unless you
are going to offer the dated name to the user, and it will add confusion as
any name the user enters in the dialog will be overwritten. For a document
template for a particular task something like the following might suffice

Dim sName As String
Dim vName As Variant
With ActiveDocument
If Len(.Path) = 0 Then
.Save
sName = .name
'or instead of the previous 2 lines
'sName = InputBox("Enter filename (without the date)")
Else
vName = Split(.name, ".")
sName = Left(vName(0), Len(vName(0)) - 11)
End If
With Dialogs(wdDialogFileSaveAs)
.name = sName & " " & Format(Date, "yyyy-mm-dd")
.Show
End With
End With

If you want something a little more universally applicable - see
http://www.gmayor.com/save_numbered_versions.htm


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

janey

Thanks for the suggestions.

I have tried the last two solutions but, in each case, got:

Document1.docx 2101-03-01

What I really want is:
Create new document (based on a particular template)
Save
Document1 2010-03-01.docx

Not sure how to transpose the item in the macro.
 
D

Doug Robbins - Word MVP

What do you really want in place of Document1? Is the user intended to
supply the name of the document to replace that?

--
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
 
J

janey

I want to create a document and save with the document name which I give it,
followed by the date.
 
G

Graham Mayor

That is because you had already saved the document as Document1.docx.

It will only work correctly if the document has not already been saved, or
if it has been saved c/w date with the macro in the format you asked for.
The macro cannot guess what your existing document name is. You have to meet
it half way. Unless you know what the original name is, so that you can
extract from it the parts you want (here I have removed the date from the
existing filename) then you cannot hope to name it as you require.

If you simply want to remove the extension from the filename before saving a
dated version of it then change the line

sName = Left(vName(0), Len(vName(0)) - 11)
to
sName = vName(0)

If you want to cater for every eventuality then we need to know what those
eventualities are.

However, you said earlier that this was for a specific document template. If
you create a new document from that template, then the macro I supplied will
work.
If you then use the macro to save again, it will remove the existing date
from the filename and save with the current date. If the current date is the
same as the original date then the original file will be over-written.

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Gordon

janey said:
I want to create a document and save with the document name which I give
it, followed by the date.

Well as you are typing in the name of the document, isn't it just as easy to
type in the date at the same time? And why would you need the date in the
name in the first place?
The Created Date field will tell you when the document was created...
 
G

Graham Mayor

It is the integration of your name and the date using existing dialogs that
is the problem. Try

With Dialogs(wdDialogFileSaveAs)
.name = InputBox("Name") & " " & Format(Date, "yyyy-mm-dd")
.Show
End With

Enter the name you want to use in the inputbox and the name and date will be
added to the saveas dialog box

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

janey

Yes, Gordon, that is the way I have done it up to now but having read the
first post in this thread, it gave me the idea that it could be automated.
The files in a particular directory in my office are listed as below:

Smith 2010-01-23
Smith 2010-01-31
Smith 2010-02-05
Voce 2010-01-14
Voce 2010-01-25
Voce 2010-02-24
etc

I just thought it would be good to be able to save/save as Smith, Voce, etc,
when I create a new document and have the date inserted.

I realise that the created date shows in the Details list but we need each
file to have the date included.
 
D

Doug Robbins - Word MVP

Running a macro with this code

Dim Fkill As String
Dialogs(wdDialogFileSaveAs).Show
With ActiveDocument
Fkill = .FullName
.SaveAs .Name & " " & Format(Date, "yyyy-mm-dd")
End With
Kill Fkill

Will display the FileSaveAs dialog and if you enter a name such as Smith
into that dialog and click OK, it will save the document with the initially
with the name Smith.doc, then it will save a copy of the document with the
name Smith 2010-03-01.doc and then it will delete the original file
Smith.doc

--
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
 
J

janey

Hi Doug, I used that macro and got:

Smith.docx

When I opened it and saved it again, I got two 'Save As' boxes. I saved it
twice and it saved as Smith 2010-03-03.docx (which is what I want) but did
not delete the original Smith.docx.

How do I get the macro to run automatically on a new file saved for the
first time, please?
 
D

Doug Robbins - Word MVP

Name the macro FileSaveAs

It will then run when you use the File>SaveAs command.

--
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
 

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