Sub SaveNumberedVersion()
'Graham Mayor 15 Jan 2006
'Revised 21 Jan 2006 to store data in registry
'Revised 25 June 2007 to impriove compatibility with Word 2007
'Loosely based on code by Doug Robbins
Dim WSHShell, RegKey, rkeyWord, Result
Set WSHShell = CreateObject("WScript.Shell")
Dim iCount As Integer
Dim strDate As String
Dim strPath As String
Dim strFile As String
Dim strFileType As WdDocumentType
Dim strVersionName As String
Dim intPos As Integer
Dim sExt As String
strDate = Format((Date), "dd MMM yyyy")
'Check whether
this is Word 2007 and apply relevant settings
If Application.Version = 12
Then
sExt = ".docx"
strFileType = wdFormatXMLDocument
Else
sExt = ".doc"
strFileType = wdFormatDocument
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
If intPos = 0 Then 'No version number
intPos = InStrRev(strFile, ".doc") 'Mark the
extension instead
End If
strFile = Left(strFile, intPos - 1) 'Strip the
extension or version number
Start: 'Get
Registry Data
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Settings\"
On Error Resume Next 'No entry in registry will
flag an error
rkeyWord = WSHShell.RegRead(RegKey & strFile)
If rkeyWord = ""
Then 'Registry entry does not
exist
WSHShell.regwrite RegKey & strFile, 0 'So create
it
GoTo Start:
End If
iCount = Val(rkeyWord) + 1
'Increment number
WSHShell.regwrite RegKey & strFile, iCount 'And
write it to the registry
'Define the new version filename
strVersionName = strPath & "\" & strFile & " - " & strDate & _
" - Version " & Format(iCount, "00#") & sExt
'and save a copy of the file with that name
ActiveDocument.SaveAs strVersionName ', strFileType
Exit Sub
CancelledByUser:
'Error handler
MsgBox "Cancelled By User", , "Operation Cancelled"
End Sub