June 12, 2017 at 9:21 am #21935
I’m writing a macro which searches a bunch of files for a pattern, gathers information and logs the results. I’d like it to run even if you have a few other files open — saved, unsaved, untitled, whatever — and then return to that former state. So I get editor.Documents.Count right away, and I assume the next two new windows I open (one for the log file, one for the pattern-search results and each source file) will have Item indices that are one higher and two higher — until recently that has been a safe assumption. But after careful debugging I find that windows’ indices change! For example, if I have open the macro I’m working on, when I open new two windows (as items #2 and #3), populate and save the first as the log file, then open the first source file into the second, the macro window becomes Item #3, when it used to be Item #1. So when I want to activate the source file I do editor.Documents.Item(3).Activate, but I get the macro window,
So two wishes: #1, I wish there were a way to activate a document by its name, not its number, and #2, Documents-collection item numbers shouldn’t change.June 12, 2017 at 10:19 am #21937
Indices change. I recommend using/saving the document object itself to distinguish among others. For instance,
doc = editor.Documents.Item(1);
if you have this “doc” object, you can use this doc for later use.June 12, 2017 at 12:06 pm #21938
OK — that’s probably a better way to code it. While I disagree with indices changing, I’ll accept it, but I’d like it clarified in the documentation for Documents.Item. Also it’s unclear that Documents Item indices go from 1 to Documents.Count, rather than 0 to Documents.Count-1.
Finally, I still with there were a way to activate a document by using its name, e.g., editor.Documents.Item(“MyDoc.txt”).Activate(). Check out how Excel uses either names or numbers to index its Workbooks, Worksheets, Charts, etc., collections. It would make for shorter and more reliable code.
You must be logged in to reply to this topic.