Results 1 to 5 of 5

Thread: Word-When macro shows a hidden form, visible Word document changes

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    4

    Word-When macro shows a hidden form, visible Word document changes

    The elements

    I have a UserForm: Userform1.
    Userform1 only has a command button: cmdHide.
    The only code in cmdHide is:
    Me.Hide

    I have a VBA macro: Macro1.
    The only code in the macro is:
    Userform1.show

    I have two Word documents open: Doc1 and Doc2.
    Doc1 is at the front (that is, it’s the visible document on the screen).

    The action

    Now, I do the following:
    I run Macro1. The macro loads and shows Userform1.
    I click on cmdHide. The macro hides Userform1 and leaves.
    Now I bring Doc2 to the front.
    I run Macro1 again. The macro BRINGS Doc1 TO THE FRONT, and then shows the hidden Userform1.

    The problem

    My problem is, I don’t want Doc1 brought to the front, I want Doc2 to stay at the front.

    Comments and additional information

    -It seems that, when a macro has to show a form that has been hidden, it first looks through the collection of open documents. If the document that was at the front when the form was loaded is in the collection, then it brings that document to the front; if it’s not, then it leaves the document that is already there. In this case, it finds Doc1, so it brings it to the front. If I had closed Doc1 BEFORE running Macro1 for the second time, then the macro would have left Doc2 at the front and would have shown the hidden form.

    -When I say “show the hidden form”, I mean I am sure it is showing the hidden form, and not loading it again.

    -In this example, I only referred to two documents, but this behavior occurs no matter how many documents are open.

    -At all times through this process, the “ActiveDocument” object retains its correct values. Even after “The macro BRINGS Doc1 TO THE FRONT”, as I said further up, ActiveDocument points to Doc2.

    Does anybody out there know why this happens, and how I can solve it?
    Thank you very much!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word-When macro shows a hidden form, visible Word document changes

    as userform is in doc1 showing the userform also activates doc1

    in the userform activate try doc2.activate

    why are you trying to do like this?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    4

    Re: Word-When macro shows a hidden form, visible Word document changes

    Hi, westconn1! Thanks for your answer!
    First of all, I've just realized I made a BIG mistake with the thread's title: it's not MsOf03 but MsOf07.

    "as userform is in doc1 showing the userform also activates doc1"

    I agree that there's some kind of binding between the form and the document that was at the front when the macro that loaded the form was run. However, that binding doesn't seem to be too tight, because, as I said, "If I had closed Doc1 BEFORE running Macro1 for the second time, then the macro would have left Doc2 at the front and would have shown the hidden form." Please note that I said "shown the HIDDEN form". That is, I made sure that it was displaying the hidden form, and not loading it again. So, the binding exists, but if I close the document, the form remains, and the behavior that's giving me trouble does not occur.

    "in the userform activate try doc2.activate"

    I had tried that. I sort of implied this close to the end of my first post when I said that "ActiveDocument" retained the correct value, in this case, pointing to doc2. More explicitly: I had tried "activedocument.activate" at the userform's activate event. I ran the macro one step at a time, and at that point, activedocument WAS doc2, and yet, doc1 appeared at the front. Anyway, now I tried "documents("doc2").activate -- just in case ... But it worked exactly the same way.

    "why are you trying to do like this?"

    Actually, the form I described is a simplification of the real one. The real form has a treeview and a combobox, each of which can contain hundreds of entries. It takes quite a long time to load. The contents of this form are the same, no matter which document the operator is editing at any given time. So, the idea is to load the form once, and then to use it with any other document. For example, if the user has 10 documents open, and he starts working on Doc1, he runs the macro, which LOADS the form and displays it. When he's done with it for THIS document, the macro HIDES (does not unload) the form and leaves. When the user moves on to, say, Doc7, and runs the macro again, the macro just displays the hidden form instead of loading it all over again, which would take a lot of time.
    So the situation at this point is:
    1) If I just hide the form, and display the form each time it is necessary, it works fast, but the document displayed is NOT the wanted one.
    2) If I unload and load it every time, the document displayed is the right one, but the process is much slower and inefficient.

    I see my posts have been quite long. I hope they're not confusing.
    Thank you again, and any ideas will be welcome !!

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Word-When macro shows a hidden form, visible Word document changes

    there are several things you could try, including
    doc2.windows(1).setfocus

    which if any would work correctly, would be trial and error
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    4

    Re: Word-When macro shows a hidden form, visible Word document changes

    Hi, westconn1!
    I read your message on Friday and tried "setfocus", but it didn't work. I'll keep searching ...
    Again, thanks a lot!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width