Kill File Name

P

Patrickw

I am creating a document from a program which merges data to a
template, then creates a document which is automatically named by the
program. The program allows me to run macros after the merge. I
don't like the automatic names assigned to the documents, so my macro
calls a SaveAs dialog with a suggested name (strFileName) which is
parsed from words in the document. The SaveAs dialog works
perfectly. I want to delete the automatic version of the file created
by my merge program after saving the new and improved version. I
coded as follows, but get a permission denied message when it gets to
the kill statement.

' Get old File Name and Path
strKillFileName = ActiveDocument.Name
' Create new File Name and Path
' Append Main Document Path to Client Document Directory
strFileName = "M:\Bankruptcy Clients\" + strFileName
' Append File Name to Client Document Path
strFileName = strFileName + "\" + UCase(TXTCreditorName.Value) _
+ " - Letter transmitting Agreed Order to " _
+ strDebtorGramaticalForm + ".doc"
' Save the document
With Dialogs(wdDialogFileSaveAs)
.Name = strFileName
.Show
End With
' Delete old File Name
Kill strKillFileName

What am I doing wrong?
 
J

Jay Freedman

I think that as far as the Windows file system is concerned, Word
still has the original file open -- doing a Save As to another name
doesn't release the file handles or the associated temp files.

If you can do an ActiveDocument.Close without exiting from Word or
stopping the macro, that may release the old file and let it be
deleted.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
P

Patrickw

I had thought Word might be telling Windows the file was still open
from my review of other posts, so I tried the following modification:

' Close the active document so the file name can be deleted
ActiveDocument.Close
' Delete old File Name
Kill strKillFileName
' Reopen the document
Documents.Open FileName:=strFileName

It works, but seems a bit kludgy. The macro is stored in normal, so
it is still available. Seems like there should be a better way to
release the file handles and associated temp files.
 
J

Jay Freedman

I had thought Word might be telling Windows the file was still open
from my review of other posts, so I tried the following modification:

' Close the active document so the file name can be deleted
ActiveDocument.Close
' Delete old File Name
Kill strKillFileName
' Reopen the document
Documents.Open FileName:=strFileName

It works, but seems a bit kludgy. The macro is stored in normal, so
it is still available. Seems like there should be a better way to
release the file handles and associated temp files.

You could look at Graham Mayor's code under "Save document to two
locations" in http://www.gmayor.com/automatically_backup.htm. It uses
FileCopy instead of SaveAs. But it still needs to close and reopen
documents, so it's no less kludgy -- that's just the nature of the
interaction between Word and Windows.
 

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