PDA

View Full Version : [SOLVED:] Rename file with sistem date and DOCVARIABLE



silvin
06-28-2015, 03:15 AM
Hi everyone,

I am trying to set a macro which renames my file . I want my document name to contain the time and a variable (or more) from my document, such as author, subject, etc.

So the name of my file should be like this : yyyy_mm_dd_hh_mm_ss DOCVARIABLE "Subjecto" DOCVARIABLE "Author"

Untill now I have this code:

Sub SaveWithDate()
If ActiveDocument.Name = ActiveDocument.FullName Then
If Not Application.Dialogs(wdDialogFileSaveAs).Show Then Exit Sub
End If
CurrentFormat = ActiveDocument.SaveFormat
CurrentPathAndName = ActiveDocument.FullName
CurrentPath = Replace(ActiveDocument.FullName, ActiveDocument.Name, "")
NewNameSamePath = CurrentPath & Format(Now, "yyyy_mm_dd_hh_mm_ss") & ""
ActiveDocument.SaveAs FileName:=NewNameSamePath, FileFormat:=CurrentFormat
Kill CurrentPathAndName
End Sub


How can I insert the DOCVARIABLE in the file name?

Thank you all in advance for your help.

gmaxey
06-28-2015, 04:37 AM
Subject and Author are built-in document properties not document variables:


Sub SaveWithDate()
Dim CurrentFormat, CurrentPathAndName, CurrentPath, NewNameSamePath
If ActiveDocument.Name = ActiveDocument.FullName Then
If Not Application.Dialogs(wdDialogFileSaveAs).Show Then Exit Sub
End If
CurrentFormat = ActiveDocument.SaveFormat
CurrentPathAndName = ActiveDocument.FullName
CurrentPath = Replace(ActiveDocument.FullName, ActiveDocument.Name, "")
ActiveDocument.BuiltInDocumentProperties("Author").Value = "silvin"
NewNameSamePath = CurrentPath & Format(Now, "yyyy_mm_dd_hh_mm_ss") & " - " & ActiveDocument.BuiltInDocumentProperties("Author").Value
ActiveDocument.SaveAs FileName:=NewNameSamePath, FileFormat:=CurrentFormat
Kill CurrentPathAndName
End Sub

silvin
06-28-2015, 04:45 AM
Thank you gmaxey. My mistake. I'm -1 to VBA.

Thank you very much for your help!

gmaxey
06-28-2015, 04:50 AM
My pleasure. Glad to help.