View Single Post
  #5   Report Post  
Posted to microsoft.public.word.docmanagement
Bouncinalong Bouncinalong is offline
external usenet poster
 
Posts: 4
Default Problem deleting old Word file after save as to new file name

Gordon,

Thanks for the response! I am running into this issue with Word 2003 and
2007 on Microsoft Xp. The wierd part is that it happens on some machines but
not others. My machine has no problems but others do (not all others). I
use the following code to do the file manipulation:

' Save the document
' This is necessary because the network may not release the file handle
' which will cause problems when we try to delete the file from
' the In Process folder
ThisDocument.Save
' Save the doc in the appropriate folder (Complete)
' NOTE: The file will be saved in the completed folder by Cust. Name, RMA
' Number, Part Number, and Serial Number
ThisDocument.SaveAs (tmpFilePath & ThisDocument.FormFields("Text15").Result
& "_" & ThisDocument.FormFields("Text8").Result & "_" &
ThisDocument.FormFields("Text7").Result & "_" &
ThisDocument.FormFields("Text9").Result)
' See if the form is in the "In Process" folder and delete it
' Because the file may not be in the folder (the user finished it out the
first time)
' we need to check before trying to delete it
If objFileSystem.FileExists(tmpFilePath2 &
ThisDocument.FormFields("Text8").Result & "_" &
ThisDocument.FormFields("Text7").Result & "_" &
ThisDocument.FormFields("Text9").Result & "_" &
ThisDocument.FormFields("Text15").Result & ".doc") Then
' Delete the file
objFileSystem.DeleteFile (tmpFilePath2 &
ThisDocument.FormFields("Text8").Result & "_" &
ThisDocument.FormFields("Text7").Result & "_" &
ThisDocument.FormFields("Text9").Result & "_" &
ThisDocument.FormFields("Text15").Result & ".doc")

End If ' objFileSystem.FileExists(tmpFilePath2 &
ThisDocument.FormFields("Text8").Result & "_" &
ThisDocument.FormFields("Text7").Result & "_" &
ThisDocument.FormFields("Text9").Result & "_" &
ThisDocument.FormFields("Text15").Result & ".doc")

I realize that these lines of code are a bit long and hard to read due the
method in which I assemble the filename. Sorry 'bout that. I kludged in the
initial "Save" as an attempt to get Word to release the file pointer but it
doesn't help (I will take that line out on the next revision). I have also
tried skipping the "FileExists" check and going straight to the delete and it
still fails. We are currently performing a manual delete after ending the
macro in the cases where it fails.

I plan to do an experiment with Excel and others over the weekend which may
prove a systemic issue and not specific to Word. I will update this post
with the results.

Thanks so much!!!!
--
Good Times!


"Gordon Bentley-Mix on news.microsoft.com" wrote:

Some versions of Word do indeed maintain pointers to files - handles, as you
call them - even after the file is closed, and these pointers are only
released when Word is shut down completely. (This is one of the reasons why
many experienced Word users recommend against editing documents on removable
media, such as flash drives; these unreleased pointers can cause file
corruption when the removable media is... well... removed.) The "strength" of
these pointers seems to vary between versions, with newer versions
maintaining a stronger grip than older versions.

Can you spot any differences between versions of Word and/or operating
system on the computers that don't allow the file to be deleted vs those that
do? In addition, can you provide more detail (in the form of a code snippet
preferrably) on how your macro deletes the old file?
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!


"Bouncinalong" wrote:

I have a macro that saves a Word form as another filename (SaveAs) in another
folder on a network drive and I use the filesystemobject to delete the old
file from the original folder after the "save as" is complete. It works fine
on some PC's but others won't let the file be deleted. It appears that Word
still has a handle to the original file after the save as is complete which
won't let me delete the old file (I also can't delete them manually using
Windows Explorer until I close the instance of Word). Shouldn't Word let go
of the original file after a "save as" is completed? Is this a registry
issue?

Thanks for any help!
--
Good Times!