RE: Access denied error when trying to delete a file - Word 2003 by Tim
Tim
Thu May 15 07:33:00 PDT 2008
I am adding some code with the hope that some of the experts will take at look
at it. The error is very inconsistent, occurring very intermittently. I am
wondering
if the network is somehow not releasing the hold on the old document even
though
Word no longer has it open.
Appreciate all comments.
Tim
*********************************************************
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
'SmartFName 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
'Delete file
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
*********************************************************
"Tim" wrote:
>
> I have developed a template that is used by greater than 100 users
> that is used for a particular type of document.
> To ensure consistent file naming, I have developed a routine that determines
> a filename based upon the document variables for each file (called
> SmartFName). I have tied this routine into FileSave.
>
> The routine performs the following:
> 1. Checks to ensure that the document variables used for the file name are
> set.
> 2. Determines the SmartFName based upon the document variables.
> 3. Compares the SmartFName to the existing filename.
> 4. If the same, saves the file.
> 5. If not the same, it prompts the user asking if the SmartFName should be
> applied.
> 6. If No, it saves the file.
> 7. If Yes, it performs a ActiveDocument.SaveAs to the SmartFName.
> 8. It then prompts the user asking if the old file should be deleted.
> 9. If Yes, a Kill oldpath filename is performed.
> It is at this point, an "Access Denied" error is generated sometimes.
>
> Thanks in advance for all help.
>
> --
> Tim