Answered by:
Title

Question
-
Hi,is it possible to have word autoname a file with the current date and also add the title of the document as the name of the file.
I done that on excel though vb but Im not sure this will be doable in word.
Wednesday, February 6, 2013 8:03 PM
Answers
-
Well, that's not what your original code did - even your code's ccomments say it's supposed to work the way it does now. All I've done is to modify the code to supply the date format.
If you want to have the pop-up appear whenever the name doesn't match the new string, try changing:
If .Path = "" Then
to:
If Left(.Name, InStrRev(.Name, ".") - 1) <> StrName ThenAlternatively, if you want the pop-up to happen every time, use:
Sub FileSave()
Dim StrName As String
With ActiveDocument
StrName = Trim(.Bookmarks("company").Range.Text) & "_" & _
Format(Now, "MM_DD_YYYY") & "_" & _
Trim(.BuiltInDocumentProperties("author"))
With Dialogs(wdDialogFileSaveAs)
.Name = StrName
.Show ' the suggested name will be in the dialog
End With
End With
End SubCheers
Paul Edstein
[MS MVP - Word]- Marked as answer by juanpablogallardo Thursday, February 14, 2013 6:55 PM
- Unmarked as answer by juanpablogallardo Thursday, February 14, 2013 6:55 PM
- Marked as answer by juanpablogallardo Thursday, February 14, 2013 6:55 PM
Thursday, February 14, 2013 5:07 AM -
I think I did it:
Sub FileSave()
Dim StrName As String
With ActiveDocument
StrName = "SOP_" & Trim(.Bookmarks("title").Range.Text) & "_" & _
Format(Now, "YYYY_MM_DD") & ""
'Trim (.BuiltInDocumentProperties("author"))
With Dialogs(wdDialogFileSaveAs)
.Name = StrName
.Show ' the suggested name will be in the dialog
End With
End With
End SubThank you so much.
- Marked as answer by juanpablogallardo Thursday, February 14, 2013 6:56 PM
Thursday, February 14, 2013 6:55 PM
All replies
-
Its doable, and pretty easy too.
String name = ActiveDocument.BuiltInDocumentProperties(1) + " - " + Date$ String directory = Environ("USERPROFILE") + "\Documents" ActiveDocument.SaveAs2 FileName:=directory + name, AddToRecentFiles:=False
Setup the name of the document, get a working directory (probably won't work on XP, not sure) and finally save the document without adding it to the recent files list.
Hope this can help.
Edit: I sorta assumed you'd be looking for a VBA based solution yet now I begin to realize that you might actually have been looking for something else. Hope this can still help.
With kind regards, Peter
Thursday, February 7, 2013 4:32 AM -
Where do I add it, to a module or to the object?
I need to run something like this, but I cant make it work because I dont know where to put it.
' A macro with the specific name FileSave is called
' whenever the user saves a document attached to
' this template.
Sub FileSave()
If ActiveDocument.Path = "" Then
' If the document has never been saved, the
' value of its .Path is an empty string; otherwise
' it has the file's path and name.
With Dialogs(wdDialogFileSaveAs)
.Name = MakeDocName ' call the function below
.Show ' the suggested name will be in the dialog
End With
Else
' The document has already been saved with a name
' so just save it there.
ActiveDocument.Save
End If
End Sub
Function MakeDocName() As String
Dim theName As String
Dim uscore As String
uscore = "_"
With ActiveDocument.Bookmarks
' Use any combination of bookmarks and
' document properties to assemble the
' suggested name.
theName = Trim(.Item("company").Range.Text)
theName = theName & uscore & Trim(.Item("date").Range.Text)
theName = theName & uscore & _
Trim(ActiveDocument.BuiltInDocumentProperties("author"))
End With
MakeDocName = theName ' return the assembled name
End FunctionFriday, February 8, 2013 6:46 PM -
Ok I got it to work thank you.
It works with the bookmark on title, that is fine.
How do I change the date field to be actually the current date on the system with the following date format:
2013_08_02
So the file i need has to be and always leading the word SOP at the beginning
SOP_title_2013_08_02.docx
Friday, February 8, 2013 7:03 PM -
Without all the circumlocution:
Function MakeDocName() As String
With ActiveDocument.BuiltInDocumentProperties
MakeDocName = Trim(.Item("Company")) & "_" & Format(Now, "MM_DD_YYYY") & "_" & Trim(.Item("Author"))
End With
End FunctionNote: The above assumes the 'Company' name has been included in the BuiltIn Document Properties. This would be your company name. The alternative, if you're after the client name, is to use the 'Client' BuiltIn Document Property.
Cheers
Paul Edstein
[MS MVP - Word]Saturday, February 9, 2013 7:02 AM -
great thank you, im actually using the bookmarks instead of the doc properties.
But Im ahving a problem, because if the file is saved, the code is not running, how do i Change that?
Tuesday, February 12, 2013 11:58 PM -
Your code, which I've simplified below to eliminate the function, has an If test that says to simply re-save the file if it has already been saved, without invoking the File|SaveAs dialogue. If the file hasn't been saved before, pressing Ctrl-S or Alt-F, S, should cause the populated dialogue to show.
Sub FileSave()
' If the document has never been saved, its .Path is an empty string;
' otherwise it has the file's path and name.
Dim StrName As String
With ActiveDocument
StrName = Trim(.Bookmarks("company").Range.Text) & "_" & _
Format(Now, "MM_DD_YYYY") & "_" & _
Trim(.BuiltInDocumentProperties("author"))
If .Path = "" Then
With Dialogs(wdDialogFileSaveAs)
.Name = StrName
.Show ' the suggested name will be in the dialog
End With
Else
' The document has already been saved with a name so just save it there.
.Save
End If
End With
End Sub
Cheers
Paul Edstein
[MS MVP - Word]- Edited by macropodMVP Wednesday, February 13, 2013 12:40 AM Code revision
Wednesday, February 13, 2013 12:34 AM -
thanks
im using 2010 and it does not work.
Wednesday, February 13, 2013 3:57 AM -
It works for me with Word 2010. Did you install the macro in the 'ThisDocument' module of the document or its template? If you did, perhaps you have other addins that are conflicting with the macro.
Cheers
Paul Edstein
[MS MVP - Word]Wednesday, February 13, 2013 4:12 AM -
Yes i did, when i run it nothing happensWednesday, February 13, 2013 4:35 PM
-
That suggests the document has already been saved. The macro only does something in documents that have not been saved.
Cheers
Paul Edstein
[MS MVP - Word]Wednesday, February 13, 2013 11:45 PM -
Yes thank you but that is precisely what i dont want, if the user hits
save or save as, the dialog should always open with the filename
showing the bookmarks im using and also the current date, that way the
user knows that the file name has been saved correctly.Thursday, February 14, 2013 4:29 AM -
Well, that's not what your original code did - even your code's ccomments say it's supposed to work the way it does now. All I've done is to modify the code to supply the date format.
If you want to have the pop-up appear whenever the name doesn't match the new string, try changing:
If .Path = "" Then
to:
If Left(.Name, InStrRev(.Name, ".") - 1) <> StrName ThenAlternatively, if you want the pop-up to happen every time, use:
Sub FileSave()
Dim StrName As String
With ActiveDocument
StrName = Trim(.Bookmarks("company").Range.Text) & "_" & _
Format(Now, "MM_DD_YYYY") & "_" & _
Trim(.BuiltInDocumentProperties("author"))
With Dialogs(wdDialogFileSaveAs)
.Name = StrName
.Show ' the suggested name will be in the dialog
End With
End With
End SubCheers
Paul Edstein
[MS MVP - Word]- Marked as answer by juanpablogallardo Thursday, February 14, 2013 6:55 PM
- Unmarked as answer by juanpablogallardo Thursday, February 14, 2013 6:55 PM
- Marked as answer by juanpablogallardo Thursday, February 14, 2013 6:55 PM
Thursday, February 14, 2013 5:07 AM -
Brilliant,
Can I remove the docproperties and have the file with this format:
Always leading the letters "SOP_" on front of the title and then the date.docx
SOP_{bookmark_"title:}_{MM_DD_YYYY].docx?
Thank you so much.
Thursday, February 14, 2013 6:51 PM -
I think I did it:
Sub FileSave()
Dim StrName As String
With ActiveDocument
StrName = "SOP_" & Trim(.Bookmarks("title").Range.Text) & "_" & _
Format(Now, "YYYY_MM_DD") & ""
'Trim (.BuiltInDocumentProperties("author"))
With Dialogs(wdDialogFileSaveAs)
.Name = StrName
.Show ' the suggested name will be in the dialog
End With
End With
End SubThank you so much.
- Marked as answer by juanpablogallardo Thursday, February 14, 2013 6:56 PM
Thursday, February 14, 2013 6:55 PM -
The file conatins macro so its a DOTM but needs to be saves as DOCX
How do I change that?
Thursday, February 14, 2013 9:29 PM -
You last two questions concern entirely different issues. Please start a new thread for each one, rather than changing the scope of this thread.
Cheers
Paul Edstein
[MS MVP - Word]Thursday, February 14, 2013 9:50 PM