Help me delete after saveas!

B

Brian

Hi,

I have seen many posts on this problem, but have not found an acceptable
solution...

I am automating word from VB and need to make changes to a document that I
cannot save. My code saves the changes to a new document using 'saveas'.
My problem is that this new file cannot be deleted until I close the word
application object.

I have tried to delete the file using the filesystem-object, the 'kill'
command and even in windows explorer to no avail. How can I get word to
remove its references to this document without closing down the application
object?

the essence of my code is this...

doc1.saveas "somenewfilepath.doc"
doc1.close false
doc2.application.selection.insertfile "somenewfilepath.doc"

kill "somenewfilepath.doc" 'this fails with permission denied error

TIA
-Brian
 
D

Doug Robbins - Word MVP

Hi Brian,

Why save the document in the firstplace?

Get the data from it and put it in Doc2 and then just close Doc1 without
saving it.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

dsc

the essence of my code is this...
doc1.saveas "somenewfilepath.doc"
doc1.close false
doc2.application.selection.insertfile "somenewfilepath.doc"

close "somenewfilepath.doc" here
kill "somenewfilepath.doc" 'this fails with permission denied error

TIA
-Brian
 
B

Brian

Hi Doug,

I am creating a COM object that exposes all of the different methods for
getting data into word (insertfile, paste, and all of the pastespecial
options). I would like to support Insert without having conditions on when
it can be used.

Thanks,

-Brian


Doug Robbins - Word MVP wrote in message ...
 
S

Steve Lang

Hi Brian,

I think you may have to come up with a different method. I also barked up
this tree several years ago, in that I was using SaveAs to save a
locally-created file to a network share. Trying to delete the original
ALWAYS failed until the current instance of Word was closed, since it
maintained a persistent lock on the file.
My work around included writing a text file of the original documents when I
did the SaveAs that was then queried by a VB exe running in the background
constantly attempting to delete any local files on the kill list.

HTH and have a great day!

Steve
 
B

Brian

Thanks for the response steve,

I have been thinking of using a similar approach if nothing else comes up.

Steve Lang wrote in message ...
 
R

Rick Wilson

Brian,
If the macro is in Normal.dot or an addin then use:

Document.close

Document.open Filename:= Name and path of your saveas file

Kill Name and path of original document

Document.close

I may have the syntax incorrect at the document.open line

All you need to do is close the SaveAs file because it has reference to the
original document until closed. If the macro is in a template outside of
Normal.dot or an addin (i.e. in a special template that the original
document is built on) then the above will not work. When you close the
document, the macro will terminate. I learned all of this a few days ago.

Hope this helps.


From: "Brian" <bc_foster[No SPAM]@hotmail.com>
Newsgroups: microsoft.public.word.vba.beginners
Date: Wed, 29 Oct 2003 14:13:14 -0500
Subject: Re: Help me delete after saveas!

Thanks for the response steve,

I have been thinking of using a similar approach if nothing else comes up.

Steve Lang wrote in message ...
Hi Brian,

I think you may have to come up with a different method. I also barked up
this tree several years ago, in that I was using SaveAs to save a
locally-created file to a network share. Trying to delete the original
ALWAYS failed until the current instance of Word was closed, since it
maintained a persistent lock on the file.
My work around included writing a text file of the original documents when I
did the SaveAs that was then queried by a VB exe running in the background
constantly attempting to delete any local files on the kill list.

HTH and have a great day!

Steve
 
B

Brian

Thanks Rick,

I tried your suggestion and still got the 'permission denied' error. My
code is running in a activeX dll. It seems that in my situtation, the
file-reference is not released until the word application is closed.


Rick Wilson wrote in message ...
Brian,
If the macro is in Normal.dot or an addin then use:

Document.close

Document.open Filename:= Name and path of your saveas file

Kill Name and path of original document

Document.close

I may have the syntax incorrect at the document.open line

All you need to do is close the SaveAs file because it has reference to the
original document until closed. If the macro is in a template outside of
Normal.dot or an addin (i.e. in a special template that the original
document is built on) then the above will not work. When you close the
document, the macro will terminate. I learned all of this a few days ago.

Hope this helps.


From: "Brian" <bc_foster[No SPAM]@hotmail.com>
Newsgroups: microsoft.public.word.vba.beginners
Date: Wed, 29 Oct 2003 14:13:14 -0500
Subject: Re: Help me delete after saveas!

Thanks for the response steve,

I have been thinking of using a similar approach if nothing else comes up.

Steve Lang wrote in message ...
Hi Brian,

I think you may have to come up with a different method. I also barked up
this tree several years ago, in that I was using SaveAs to save a
locally-created file to a network share. Trying to delete the original
ALWAYS failed until the current instance of Word was closed, since it
maintained a persistent lock on the file.
My work around included writing a text file of the original documents
when
I
did the SaveAs that was then queried by a VB exe running in the background
constantly attempting to delete any local files on the kill list.

HTH and have a great day!

Steve


Hi,

I have seen many posts on this problem, but have not found an acceptable
solution...

I am automating word from VB and need to make changes to a document
that
I
cannot save. My code saves the changes to a new document using 'saveas'.
My problem is that this new file cannot be deleted until I close the word
application object.

I have tried to delete the file using the filesystem-object, the 'kill'
command and even in windows explorer to no avail. How can I get word to
remove its references to this document without closing down the
application
object?

the essence of my code is this...

doc1.saveas "somenewfilepath.doc"
doc1.close false
doc2.application.selection.insertfile "somenewfilepath.doc"

kill "somenewfilepath.doc" 'this fails with permission denied error

TIA
-Brian
 

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