Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Sep 2002
    Location
    Detroit, Michigan, USA
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Disabling code with code (MSWord97/VBA)

    I created a substantial number of documents with a custom template that has code in the Doc_Open event that shows an introductory form. I need to find out whether is possible to disable this form upon loading of the doc via VBA is order to make a mass programatic change to all the docs thru code without having to close manually the form each and everytime the new doc is opened. Thanks in advance.

  2. #2
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Disabling code with code (MSWord97/VBA)

    If all the docs are based on just one .dot template, and you are going to be doing the mods all at the same time, just open the template and rename the Document_Open procedure to something else.

    Run your updates and then change the name back.

    The Code from templates stay in the templates and don't go over to the documents themselves.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Disabling code with code (MSWord97/VBA)

    The Shift key will disable AutoOpen. I think it also will disable the Document_Open event handler. Therefore, in theory, if you get some industrial strength tape and tape down the shift key while your macro runs, it might work just fine.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Disabling code with code (MSWord97/VBA)

    It's a little unclear from your post what you're trying to do, but since you do mention needing to suppress the showing of the form (technically, the userform) via VBA, here's a different take on this:

    If I understand it right, you've got code in the Document_Open event procedure, which makes a userform show. Typically, the documents get opened manually by users, so there's no problem with showing the userform, 'cause the user can interact with or dismiss the userform manually.

    However, there are other times when you want to open and modify these documents programatically; in these instances, you currently have a problem because the userform is displaying, and there's no practical way to dismiss the userform programatically, once it's showing.

    There is at least one way to achieve this, but it does involve some reorganization of things.
    First off, the assumption is that the code you are using to open and modify these documents programaticaly, is residing in a global template.
    Next, in any standard code module in the global template, declare a global variable such as "Public gboolShowUserFormIsSuppressed As Boolean".
    Next, you need to modify the template that the documents are based on, and which contains the userform and the code to show it in the Document_Open event procedure:
    First, using Tools > References, set a reference to the global template - this will make public variables and procedures which are located in the global template, available to the local template as well.
    Next, in the Document_Open code, wrap the statement which shows the userform with an If test like this:

    If Not gboolShowUserFormIsSuppressed Then MyUserform.Show

    And finally, in the code procedure in the global template, which opens and modifies these documents programatically, you would need to have code toward the beginning of the procedure like: "gboolShowUserFormIsSupressed = True", and at the end of the procedure like "gboolShowUserFormIsSuppressed = False".

    The way this works:
    (We're assuming the global template is in the startup directory so that it loads when Word launches)
    When Word launches and the global template loads, any public variables in the template are initialized.
    Boolean variables initialize with a value of False, so until something actively changes this value, the value of gboolShowUserFormIsSuppressed will be False.
    Because of this, in normal circumstances such as when a user opens a document based on the local template, the If test in the Document_Open procedure will determine that the boolean variable's value is False, and the userform will show as normally.
    However, when you open the document programatically via the code in the global template, the value of the boolean variable is temporarily set to True, this way the If test in the Document_Open procedure will suppress the showing of the userform.

    Hope the above makes any sense and is of any use! (Regardless of whether applicable in this case, it is a very useful technique, particularly when doing things like automating Word documents.)

    Gary

  5. #5
    Star Lounger
    Join Date
    Sep 2002
    Location
    Detroit, Michigan, USA
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Disabling code with code (MSWord97/VBA)

    Thanks again for the prompt and detailed replies. I wasn't aware that the Doc_Open code still resides in the template...(I though Open and Close code migrated with the actual document). Now this raises another question...does the document always try to find the original template? There is a setting under Tools/Templates and Add-Inn's that forces styles to refresh and follow the latest changes in template styles. As for code goes...this process takes place all the time?
    Thanks again

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Disabling code with code (MSWord97/VBA)

    > does the document always try to find the original template

    Yes. I think there is a rule for where it looks, and in what sequence before it gives up, something like "original path, then UserTemplates path, then under the Office program path" etc., but I can't remember where I saw that.

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Disabling code with code (MSWord97/VBA)

    Document event code can reside in the ThisDocument component of a document.

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Disabling code with code (MSWord97/VBA)

    Just to avoid any possible confusion: while, as Howard observes, Document_Open code can exist in the ThisDocument module of the document itself, that is not the usual situation, and would not be the situation in this case:

    Assume a document which is based on a template; the template contains macro code in its ThisDocument module (as well as, potentially, in a standard code module, a class module, or in a userform's code module).
    When a new document is created based on this template, none of the code from the template is reproduced into the new document; all of the code continues to reside in the template to which the document is attached.

    As you observe, when a document opens, it always tries to find its attached template. As long as it can find its attached template (i.e. the template still resides at the specific directory location specified in the Templates and Add-Ins dialog), then the code in the attached template will still be in force with regard to the document. So for instance, code in the Document_Open procedure will run when the document is opened.

    Gary

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Disabling code with code (MSWord97/VBA)

    Actually, one of the tenets of my religion is "Thou shalt not incude any code in a document unless thou are certain it is necessary, or are of diminished capacity due to, say, drugs or alcoholic beverages."

Posting Permissions

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