Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Excel with Word, close all txt files in folder?

Status
Not open for further replies.

rudo

Programmer
Jul 3, 2002
72
NL
Hi,

In my application VBA Excel opens .txt-files in Word. These are temporary files in a specific folder and when the program starts it must kill all existing .txt-files in the folder. When one of the .txt files is still open an error occurs. Is there a way to close all open .txt files in the folder?

Set WrdApp = CreateObject("Word.Application")
???
Kill (C:\Workshop\Temporary Files\*.txt")

Thank you very much in advance!
 
VBA Excel opens .txt-files in Word
which code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I just wanted to make sure, is this a direct copy-paste from your code:

Code:
Kill (C:\Workshop\Temporary Files\*.txt")

If so, you missed the quotes at the beginning:

Code:
Kill ("C:\Workshop\Temporary Files\*.txt")
 
@PHV: Sorry for the delay in replying. Spent my day waiting for a plane that did not come. Storm.
@Phantek: I wrote this path as an example for the forum and forgot the quotation mark. In reality I use a string variable for the path and file.

@PHV: The code I use to open Word and a text file:

Set WrdApp = CreateObject("Word.Application")
Set WrdDoc = WrdApp.Documents.Open("C:\Workshop\Temporary Files\work.txt")

My application is composed by several sections between which the user prepares the needed information and then starts the next macro. Each macro contains
Set WrdApp = CreateObject("Word.Application") and opens still more txt-files.
 
Hi,

I solved the problem by putting in place a bookkeeping of all txt files that are opened and close them one by one with a routine at the end of the program.
Code:
        WrdApp.Windows(MyFile).Activate
        WrdApp.ActiveDocument.Close False
Thanks for trying to help.
 
Why not simply this ?
Code:
WrdApp.Documents.Close False

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

Thank you for your reaction.

Code:
WrdApp.Documents.Close False

This is the code I initially used, but it did not close all documents. I now think the reason for this must have been that while testing and restarting my program each time, several instances of Word were opened. Apparently, the close instruction only works with the Word-copy that opened the file.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top