Renaming loaded file and deleting the old file

T

Tim

I have previously posted this but I am still looking for a resolution

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.
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

'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
 
D

Doug Robbins - Word MVP

I think you may need to close the document before using the Kill statement,
then open it again using the SmartfName

--
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,

Thanks for the response

1. Why should the new document need to be closed? Since a
SaveAs operation has occurred, shouldn't the existing document
be released?
2. Since I ran into this issue, I developed the following for a work-around.
After the SaveAs:
a. Open a new document based upon the same template to keep
the connection to the template open so that the routine doesn't stop.
b. Close the new document.
c. Try to delete the old document using Kill and then re-open the new
document.
d. Very intermittently an "Access Denied" error is thrown.
e. I have added error code to make an entry into a text log file when this
error occurs. The time, template name (code is included in both
Procedure.dot & ProcEdit.dot templates), UserId, PathFname are all
captured in this text file.

Examples:
06/03/08 07:59:06 - Procedure.dot, MMH1, File: C:\Data\EDMS\MP_I-RF18u3r01.DOC
06/05/08 10:14:10 - ProcEdit.dot, JRH2, File:
S:\PROC\Procedur\Vol01AP\AD9!ID1u3r10.DOC
06/09/08 16:43:12 - ProcEdit.dot, JIH1, File: C:\DATA\EDMS\STP_M-16Q1u3r10.DOC
06/10/08 10:32:31 - Procedure.dot, JDS9, File:
S:\PROC\Writer\JDS9\Cmplt\STP_I-12-P935u2r08.DOC
06/10/08 12:45:40 - Procedure.dot, WKG3, File: C:\Documents and
Settings\WKG3\My Documents\B-Procedure Drafts\EDMG_EDG-5u3r01.doc
06/10/08 12:53:43 - ProcEdit.dot, DJO4, File:
S:\PROC\Procedur\Vol08CAP\CAP_A-8u3r31.DOC
06/10/08 13:17:17 - ProcEdit.dot, ALLG, File: P:\Procedure Drafts\OP G-1
Drafts\OP G-1 I Drafts, Liquid Radwaste - Make Available\OP G-1 I U1.Doc
06/10/08 15:48:29 - ProcEdit.dot, DJO4, File:
S:\PROC\Procedur\Vol06STP\STP_R-22u3r08.DOC
06/11/08 09:26:14 - Procedure.dot, MYH3, File:
S:\PROC\Procedur\Vol05MNT\00916703.DOA
06/12/08 10:59:34 - Procedure.dot, JGSO, File: C:\Data\EDMS\CAP_B-42u3r02.doc
06/12/08 11:24:58 - Procedure.dot, JGSO, File: C:\Data\EDMS\CAP_B-42 jgs
u3r03.DOC
06/15/08 04:35:01 - ProcEdit.dot, GCS1, File:
C:\Data\EDMS\OP1!DC10_-_Form_69-20397u3r08.DOC
06/17/08 13:20:34 - ProcEdit.dot, JRH2, File:
S:\PROC\Procedur\Vol02OP\OP_K-1~Iu2r13.DOC
06/17/08 13:59:42 - Procedure.dot, JEK2, File: J:\Temporary\Generator Rotor
Transport Procedure\TP_TD_0801u2r00.DOC
06/18/08 13:33:20 - Procedure.dot, MYH3, File:
S:\PROC\Procedur\Vol05MNT\453M3U05.DOA
06/18/08 15:16:07 - Procedure.dot, CMSA, File:
S:\PROC\Procedur\Vol05MNT\00903403.DOA
 

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