Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Access denied error when trying to delete a file - Word 2003

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim - 12 May 2008 16:37 GMT
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.

Signature

Tim

Tim - 15 May 2008 15:33 GMT
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
*********************************************************

> I have developed a template that is used by greater than 100 users
> that is used for a particular type of document.
[quoted text clipped - 17 lines]
>
> Thanks in advance for all help.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.