Consulting

Results 1 to 4 of 4

Thread: Rename file with sistem date and DOCVARIABLE

  1. #1
    VBAX Newbie
    Joined
    Jun 2015
    Posts
    2
    Location

    Rename file with sistem date and DOCVARIABLE

    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.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Jun 2015
    Posts
    2
    Location
    Thank you gmaxey. My mistake. I'm -1 to VBA.

    Thank you very much for your help!

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    My pleasure. Glad to help.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •