Hi, all. I don’t imagine my problems will actually be solved here, but I do hope to gather any “best practices” I may not be aware of, in hopes of minimizing them.
I developed a template, which is stored on a network share/folder, which is set on users’ machines as the “Workgroup Templates” location. It uses an Excel spreadshet that is also stored there, and which contains the values for the various picklists. (I decided it would be easier for the system owner to maintain a spreadsheet than to keep modifying code when a new product type, etc. is added.) I will note that the “initialization” of the dialog box on Word (checking for and instantiating Excel and obtaining the values from the specified ranges) goes off without a hitch. It opens the spreadsheet and collects the values it needs for the array with no problem.
The DOTM template has a very traditional dialog box, parts of which are populated by the spreadsheet mentioned above) that gather information from text boxes and list-box choices. It then plugs these values into the document a specific locations (bookmarks) using a function I wrote that maintains the bookmark after the text is inserted (by “re-bookmarking” the range.) As always happens when developing, it performs fine on my machine. But during testing, the first user reported a number of errors that I simply can’t duplicate nor predict.
For example, she initially reported that choices from the picklists threw “5941” errors. (There is some validation going on when a choice is made, i.e. some choices are mututally exclusive.) I couldn’t duplicate any of them, nor can she 24 hours later. But, today presented a different set of oddities: namely, when certain choices are made, some boilerplate (Building Block/autotext) is inserted at a named range, and a second dialog should then appear to prompt for two additional pieces of information. When testing the template this morning on her machine, I noticed that the second dialog box wasn’t appearing at all. When I started the process over, I got an error about not being able to modify a range. I chose to debug, and I saw it stopped on this line:
If blnECTD = False Then Activedocument.Bookmarks(“bmkeCTDText”).Range.Paragraphs(1).Range.Delete
(The variable blnECTD indicates whether the user chose to keep a certain paragraph via an option button in the dialog box.)
However, after verifying that the bookmark existed, I simply resumed the paused code and it indeed deleted the paragraph associated with that bookmark as if it never stopped at all. This still happens most of the times when the macro is called! (And to cloud the issue further, it started showing the second dialog box that wasn’t being triggered when I ran it a second time!)
It’s almost as if the code is stumbling over itself. Does anyone have any similar problems? I am using the ActiveDocument object, but during testing, it was the only Word file open! Should I use a reference/variable instead of the ActiveDocument object? (I didn’t think it necessary, though I usually work that way, since I’ve had problems in the past where Word “loses track” of which document to work with, especially if the “Show All Windows In Taskbar” option is turned on.) Are there any other “habits” one should get into when working with sketchy environments? (We can sometimes have painful latency issues.)
It seems the better I get at automating Word, the more unpredictable VBA reveals itself to be!