We help IT Professionals succeed at work.
Get Started

Word macro to save file with date rev and timestamp

1,299 Views
Last Modified: 2016-02-13
I have a macro which I use in word to save the file with date rev and timestamp added to the filename.

I need the macro to create a directory called superseded if one does not already exist and then to save the file in main directory and superseded directory. Then to "kill" the previous file from the main directory, and leave the current file in the directory.

BTW I have an excel macro which does this.

The macro for word is posted below:

Sub SaveNumberedVersion()
'Graham Mayor 15 Jan 2006
'Completely Revised 18 January 2011
'to store count in a document variable
'and improve document type handling

Dim strVer As String
Dim strDate As String
Dim strPath As String
Dim strNewPath As String
Dim strFile As String
Dim oVars As Variables
Dim strFileType As WdDocumentType
Dim strVersionName As String
Dim intPos As Long
Dim sExt As String
Dim strNewFolderName As String

Set oVars = ActiveDocument.Variables
strDate = Format((Date), "dd MMM yyyy")

If Len(Dir(strPath & strNewFolderName, vbDirectory)) = 0 Then
        MkDir (strPath & strNewFolderName)
End If



With ActiveDocument
    On Error GoTo CancelledByUser
    If Len(.Path) = 0 Then 'No path means document not saved
        .Save 'So save it
    End If
    strPath = .Path 'Get path
    strFile = .Name 'Get document name
End With
intPos = InStr(strFile, " - ") 'Mark the version number
sExt = Right(strFile, Len(strFile) - InStrRev(strFile, ".do"))
If intPos = 0 Then 'No version number
    intPos = InStrRev(strFile, ".do") 'Mark the extension instead
End If
strFile = Left(strFile, intPos - 1) 'Strip the extension or version number
Select Case LCase(sExt) 'Determine file type by extension
    Case Is = "doc"
        strFileType = 0
    Case Is = "docx"
        strFileType = 12
    Case Is = "docm"
        strFileType = 13
    Case Is = "dot"
        strFileType = 1
    Case Is = "dotx"
        strFileType = 14
    Case Is = "dotm"
        strFileType = 15
End Select
Start: 'Get Registry Data
On Error Resume Next 'No entry in registry will flag an error
strVer = oVars("varVersion").Value
If strVer = "" Then 'Variable does not exist
    oVars("VarVersion").Value = "0" 'So create it
    GoTo Start:
End If
On Error GoTo 0
strVer = Val(strVer) + 1 'Increment number
oVars("varVersion").Value = strVer
'Define the new version filename change version in line below to Rev if required
strVersionName = strPath & "\" & strFile & " - " & strDate & _
" - Rev " & Format(Val(strVer), "00# ") _
& Format(Now, "hh-nn") & Chr(46) & sExt

strNewPath = strPath & "\" & strNewFolderName & "\" & strFile & " - " & strDate & _
    " - Rev " & Format(Val(strVer), "00# ") _
    & Format(Time(), "hh-mm") & Chr(46) & sExt



'and save a copy of the file with that name
ActiveDocument.SaveAs strNewPath
ActiveDocument.SaveAs strVersionName, strFileType
Exit Sub

CancelledByUser: 'Error handler
MsgBox "Cancelled By User", , "Operation Cancelled"
End Sub
Comment
Watch Question
Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE