Weirdness with two docs open based on same template

G

Gary Hillerson

i have a Word VBA template with a lot of code and userforms in it, and
everything is working fine.

however, i've discovered that, if I create two documents (doc1 and
doc2) based on that same template and have both open at once, I get
strange behavior.

Specifically, whenever i call the .Show method of a userform in doc1,
all is fine. But if I call the .Show method of one of my userforms in
doc2, then doc1 becomes the ActiveDocument before that userform is
displayed. As a result, when the userform inserts text into the
document, it always gets inserted into doc1. And doc1 remains the
ActiveDocument.

I don't understand what I could be doing to change Word's idea of the
ActiveDocument, but I must be. Is there something special I need to do
in my code to handle having multiple documents open that are referring
to code in the same template?

any help/pointers appreciated.

thanks,
garyh
 
J

Jezebel

Have you checked your code carefully for document and selection references?
This kind of problem often turns out to be a result of assumptions about
document indexes (eg a reference to Documents(1) or some such). Anyway,
until you have evidence to the contrary, assume the problem is a bug in your
code. Try stepping through the code (using F8) and monitor what
ActiveDocument points to and when it changes.
 
G

Gary Hillerson

Thanks "Jezebel."

I had already done what you suggested when I wrote, but I got more
persistent after your message and finally nailed it.

I was doing a .Hide on my main userform instead of unloading it as an
"optimization." Changing it to unload fixed things, and it even makes
sense.

gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top