Thanks for sharing!
Thanks for sharing!
Karen

This month, every Windows Secrets subscriber can download a one-chapter excerpt of Windows 7: The Missing Manual.Windows 7: The Missing Manual provides valuable information to help you overcome these difficulties in learning a new operating system. Subscribe today to download your free excerpt.
(Edited by HansV to provide link to post - see <!help=19>Help 19<!/help>)
Hi, all. This is a follow-up to an earlier post of mine (<post#=381811>post 381811</post#>) where I described how Word would sometimes "lose" my document when I was manipulating it via VBA. In that instance, my code opened up a TXT file with many lines of delimited text (a log file that was written by another process and which uses dollar-signs to delimit the text for conversion into a table.) It then converted it to a table and formatted the document (or at least, it was supposed to.) What happened was that, periodically and unpredictably, the document to which I was referring in my code seemed to vanish, and much of the subsequent formatting was then applied to whatever hapless document happened to be open at the same time! This behavior was not always reproduceable, so it was very difficult to debug.
I recently was able to step through the code and actually go to see what was happening in those instances.
What I found was that, immediately after converting the delimited text-range to a table, the document (actually, the document window) had a disturbing tendency to disappear! the Window menu would act like it was there, but I couldn't use it to activate the document. If I used CTRL+F6 to cycle through the windows, Word acted like a wino at Mardi Gras, stumbling through each document window, showing some as being maximized and some in the "Restore" view. Eventually it would cycle to the "missing" window and I could see that it was still open. But this didn't help my code from manipulating the wrong document if the intended file disappeared.
Anyway, I found that if I used the line Set LogDoc = Documents.Open(LogFile) when opening the text file, I could successfully manipulate the document, even after it disappeared, by using ranges derived from the LogDoc variable.
Now, why the doc disappears in the first place is still a mystery, but this further emphasizes the value of using ranges rather than the selection object when working in VBA.
<font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike