Microsoft Word 2007 VBA-When macro shows a hidden form, visible Word document changes RRS feed

  • Question

  • The elements

    I have a UserForm: Userform1.
    Userform1 only has a command button: cmdHide.
    The only code in cmdHide is:
    There is no other code in Userform1.

    I have a VBA macro: Macro1.
    The only code in the macro is:
    The macro and the form are located in Normal.dotm.

    I have two Word documents open: Doc1 and Doc2.
    Doc1 is at the front (that is, both document windows are in fullscreen mode, and Doc1 is 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. That is, I want the document that was at the front when the macro was run to remain that way.

    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!
    Friday, July 22, 2011 3:14 PM