We help IT Professionals succeed at work.

Word macro to save file with date rev and timestamp

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

Phillip BurtonDirector, Practice Manager and Computing Consultant
Awarded 2014
Top Expert 2014

Commented:
Not to answer the question, but why do you use this format:

strDate = Format((Date), "dd MMM yyyy")

I find using the Japanese format (yyyymmdd) means that, if you have multiple files, listing them in alphabetical order would be the same as date order.
GrahamSkanRetired
Top Expert 2012

Commented:
It's a bit confusing, but try this;
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
Dim strFullName As String

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

strNewFolderName = "\superseded"
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
    strFullName = .FullName 'path & file 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
ActiveDocument.Close wdDoNotSaveChanges
Kill strFullName
Exit Sub

CancelledByUser: 'Error handler
MsgBox "Cancelled By User", , "Operation Cancelled"
End Sub

Open in new window

Author

Commented:
Gives an error at the following line:

ActiveDocument.SaveAs strNewPath

Sorry for short message in lectures all day today.
Phillip BurtonDirector, Practice Manager and Computing Consultant
Awarded 2014
Top Expert 2014

Commented:
I would suggest putting before it

MSGBOX (strNewPath)

and see what the results is.

Author

Commented:
Still having problems with this, have tried above solutions but to no avail.
Phillip BurtonDirector, Practice Manager and Computing Consultant
Awarded 2014
Top Expert 2014

Commented:
And what is the MSG you get from the MSGBOX box - or do you not get that far/

Author

Commented:
One other thing is that is doesn't make a folder called superseded, it just bypasses that step for some reason.

Author

Commented:
Hi Phillip,

I get strNewPath=""

Author

Commented:
Hi Guys,

Still having problems with this macro as below;

1 -  it doesn't make a directory called superseded it just goes through the steps without creating it
2 - gives an error message at the ActiveDocument.SaveAs strNewPath

3 - unsure whether the kill command works in Word VBA, it works in excel VBA

Any assist would be greatly appreciated
Retired
Top Expert 2012
Commented:
In my first suggestion, I put the folder creation part before the existing path had been established. This version has it later in the code.
If you get an error , tell us what the error is and the line that it occurs on. Thanks
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
Dim strFullName As String

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

strNewFolderName = "\superseded"



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
    strFullName = .FullName 'path & file name
End With
If Len(Dir(strPath & strNewFolderName, vbDirectory)) = 0 Then
        MkDir (strPath & strNewFolderName)
End If

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
Kill strFullName 'delete original file
Exit Sub

CancelledByUser: 'Error handler
MsgBox "Cancelled By User", , "Operation Cancelled"
End Sub

Open in new window