Hi all - yes I hate that "sometimes" word too. But in this case, I cannot find any good reason for this failure. Must be said upfront - the application I describe below works perfectly....until it doesn't. So, the code itself is ok. But clearly the code is doing something that it eventually doesn't like, and I get errors. I am using the latest and greatest Office 365 ProPlus versions.
My application loops through a text file, reading/pulling various bits of information, to be written to a spreadsheet. Part of that information is a Word document file name. My program needs to read the Word file for more specific information. So, my code calls a function that:
When the application is finished scanning through the .txt file, it goes to the next .txt file and repeats this process. There are 60 .txt files this application reads in this way. The whole point of the program is go through all 60 .txt files, and print the results onto a large spreadsheet. In doing so, the code will open/read/close 100s and 100s of Word docs. As I have said, it is all working....until it "dies" on me.
This works great for somewhere around 100-150 "loops", over a couple of the .txt files. side note: reading thro9ugh the .txt file is alarmingly FAST! But it slows way down in this function, apparently during the object creation, checking for folders and files and opening of the Word file (a couple seconds at least). That is probably not an issue, but throwing it out there in case anybody thinks opening a Word file (these are small files, 1 - 3 pages, minimal text) shouldn't be so slow.
But nevermind speed for the moment. While running this program, at some point, it will seem as if the program has stalled...and finally I will get an error, always within this Function. The errors are around not being able to open the file, or that Word has a a problem and can't open the file, or that Office has an issue, that maybe the file is corrupt. They are not. I have checked them, they all open and read just fine, as expected.
So, I am thinking, might their be a limit as to how many times, in short succession I can open/close a Word file? I know, doesn't make sense to me either...
Before you ask, no, it does NOT fail on the same file each time. I have stopped at this error, gone to the actual file, opened it - no problem. And, I can start and stop my application using other text files, and therefore it will be checking a whole different set of Word docs, and the error will still occur. The files are always where they should be, appear to be available to open, and not locked (I check for that possibility in the code).
Based on this description, anybody have any ideas what I might look for, or other coding methods to avoid the constant createobject/open/read/close cycle? For example, is there a way to pass the objects via the Function, so I can create only one instance of the FileSystemObject and Word.app in the main sub, then use them many times within the function? not sure I know how to do that... but....willing to learn! Open to any ideas.
Many thanks
Terry
My application loops through a text file, reading/pulling various bits of information, to be written to a spreadsheet. Part of that information is a Word document file name. My program needs to read the Word file for more specific information. So, my code calls a function that:
- creates a FileSystemObject (set FSO = CreateObject("Scripting.FileSystemObject)
- creates a word app (set wdapp = CreateObject("Word.Application")
- Using the FSO object, checks to see if the folder and file exist, if so,
- opens the Word file via wdapp
- reads/captures the specific data
- closes the Word file
- sets the file and Word app objects to "nothing"
- returns to the calling subroutine.
When the application is finished scanning through the .txt file, it goes to the next .txt file and repeats this process. There are 60 .txt files this application reads in this way. The whole point of the program is go through all 60 .txt files, and print the results onto a large spreadsheet. In doing so, the code will open/read/close 100s and 100s of Word docs. As I have said, it is all working....until it "dies" on me.
This works great for somewhere around 100-150 "loops", over a couple of the .txt files. side note: reading thro9ugh the .txt file is alarmingly FAST! But it slows way down in this function, apparently during the object creation, checking for folders and files and opening of the Word file (a couple seconds at least). That is probably not an issue, but throwing it out there in case anybody thinks opening a Word file (these are small files, 1 - 3 pages, minimal text) shouldn't be so slow.
But nevermind speed for the moment. While running this program, at some point, it will seem as if the program has stalled...and finally I will get an error, always within this Function. The errors are around not being able to open the file, or that Word has a a problem and can't open the file, or that Office has an issue, that maybe the file is corrupt. They are not. I have checked them, they all open and read just fine, as expected.
So, I am thinking, might their be a limit as to how many times, in short succession I can open/close a Word file? I know, doesn't make sense to me either...
Before you ask, no, it does NOT fail on the same file each time. I have stopped at this error, gone to the actual file, opened it - no problem. And, I can start and stop my application using other text files, and therefore it will be checking a whole different set of Word docs, and the error will still occur. The files are always where they should be, appear to be available to open, and not locked (I check for that possibility in the code).
Based on this description, anybody have any ideas what I might look for, or other coding methods to avoid the constant createobject/open/read/close cycle? For example, is there a way to pass the objects via the Function, so I can create only one instance of the FileSystemObject and Word.app in the main sub, then use them many times within the function? not sure I know how to do that... but....willing to learn! Open to any ideas.
Many thanks
Terry