Jerry,
I might be wrong, but I suspect that you might run into problems attempting
to copy an already open file.
What about copying the file each time you close the document? That would
almost be the same thing, assuming that no one deletes the copy before the
original file is opened again. At that point, a copy of the file would
already exist.
The reason for this, is that if one does a SaveAs, the name of the file
saved changes. I thought about re-opening the original file and closing the
saveAs-ed file, but then re-opening the file might trigger the macro to save
itself again.
Perhaps, someone will present a better answer. Anyway, this is what I came
up with:
'
' Save a copy of the document to the desktop on close
'
Sub AutoClose()
Dim sFilePath As String
Dim sFileName As String
Dim sNewFileName As String
Dim actDoc As Document
'
' Desktop path
'
sFilePath = "C:\Documents and Settings\Steven\Desktop\"
'
Set actDoc = ActiveDocument
sFileName = actDoc.Name
sNewFileName = UniqueFileName(sFilePath, sFileName)
actDoc.SaveAs FileName:=sNewFileName
End Sub
'
' UniqueFileName
'
' This macro creates a unique name for a document based on:
' (1) the name of the active document from which this macro was ran; and
' (2) and a two digit number starting with "01"
'
' And it also tests to see if such a document exists, if so, it increments
' the number and tries again.
'
Function UniqueFileName(ByVal sFilePath As String, _
ByVal sFileName As String) As String
Dim sNum As String
Dim sNewFileName As String
Dim nNumber As Long
Dim nPos As Long
nPos = InStrRev(sFileName, ".")
If nPos > 0 Then
sFileName = Left(sFileName, nPos - 1)
End If
Do
nNumber = nNumber + 1
sNum = CStr(nNumber)
If Len(sNum) = 1 Then
sNum = "0" & sNum
End If
sNewFileName = sFilePath & Application.PathSeparator _
& sFileName & "_" & sNum & ".Doc"
Loop While (Dir(sNewFileName) <> "" And nNumber < 1000)
UniqueFileName = sNewFileName
End Function
Steven Craig Miller