Renaming loaded file and deleting the old file

T

Tim

Word 2003

I have a routine that I have tied to FileSave that compares the
filename to a filename generated by looking at the document variables.
If the filenames do not match, the existing document is saved to the
new filename and the old file is deleted.

At times, an 'Access denied' error is occurring when the old file deletion
is attempted.

Thanks in advance for any suggestions.
 
D

Doug Robbins - Word MVP

Show us the code that you are using.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
T

Tim

Doug,

Here is the code. Appreciate your help.

Sub FileSave()
Dim dlg As Dialog, curDoc As Word.Document
Dim SmartFName As String, CurFName As String, sPath As String, iMsg As Integer
' If template or a document that hasn't been saved yet,
' show the FileSaveAs dialog
If ActiveDocument.Type = wdTypeTemplate Or ActiveDocument.Path = "" Then
Set dlg = Dialogs(wdDialogFileSaveAs)
dlg.name = ActiveDocument.name
If dlg.Show = 0 Then Exit Sub 'Cancelled out of dialog
Exit Sub
' Lib1.ValidProcedureDocument checks to ensure that the activedocument
' has the correct document variables
ElseIf Not Lib1.ValidProcedureDocument(Doc:=ActiveDocument) Then
ActiveDocument.Save
Exit Sub
End If
On Error GoTo ErrTrap
Set curDoc = ActiveDocument
sPath = curDoc.Path & "\"
CurFName = curDoc.name

'GetSmartFName derives a filename based on the document variables
SmartFName = Lib1.GetSmartFName(wrdDoc:=curDoc)
If UCase(CurFName) <> UCase(SmartFName) Then
iMsg = MsgBox("Save file with Smart File Name?" & vbCrLf & SmartFName,
vbQuestion + vbYesNo, msgTitle)
If iMsg = vbYes Then
ActiveDocument.SaveAs FileName:=sPath & SmartFName,
FileFormat:=wdFormatDocument
iMsg = MsgBox("Delete old file?" & vbCrLf & CurFName, vbQuestion +
vbYesNo, msgTitle)
If iMsg = vbYes Then
'The following is where the "Access Denied" error is generated
Kill sPath & CurFName
End If
Else
curDoc.Save 'Save with existing non-SmartFName
End If
Else
curDoc.Save 'Already has SmartFName
End If
Set curDoc = Nothing
Exit Sub
ErrTrap:
If Err.Number = 4198 Then 'Canceled from Save Dialog
Exit Sub
Else
MsgBox "Error: " & Err.Description, vbCritical, msgTitle
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top