Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA Document Code Sheet

    Hi Folks,

    I have tons more experience with Excel, and tried to carry a useful portion of that experience over into Word, unsuccessfully so far. Allow me to explain the Excel situation first:

    I create a subroutine that generates a custom toolbar on the fly. In the VBA Project Explorer, I double-click "ThisWorkbook", generate a Workbook_Open code block, and call the toolbar-generator from within. I put similar, appropriate code into Workbook_Activate, Workbook_Deactivate, etc. to cause the toolbar to only appear when this workbook is active (because the toolbar functions only apply to this workbook application). I then save the workbook as a template. When I create a new workbook from the template, the toolbar appears, as I expect and desire.

    I tried the same approach in Word. The only obvious difference I saw was, there are fewer events (New, Open and Close) associated with the "ThisDocument" object. I put calls to the toolbar builder into the Open and New events, then saved the document as a template. When I create a new document based on the template, the toolbar does not appear as expected (that is, it does not appear at all). When I go to the VBA editor and check the "ThisDocument" code page, there is no code, as I would expect.

    How do I include code in a document template that runs whenever the user creates a new document based on that template?

    Thanks!

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA Document Code Sheet

    Jim,

    Create an AutoNew attached to the template.

    That can be done by inserting a module, naming it "Autonew", and naming a routine "Sub Main". Or within a module of any name, define "sub AutoNew".
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA Document Code Sheet

    Thanks! I'll try it. Are the Document event code blocks not preserved at all in a template? If so, what is the logic behind it, if any?

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

    Re: VBA Document Code Sheet

    Hi Jim,

    The Document events certainly do work in templates - so it's a little hard to figure out why they didn't work in your template.

    One possible clue that stood out in your first note - you mention having added the Document Event code, and then saving the document as a template - I've never done it in that order, but just possibly that may have caused the problem (haven't tested it, but just possibly code in ThisDocument is getting wiped when you save the .doc as a .dot (?).

    Instead, trying creating your .dot file first, and then putting the document event code in. Worth a try anyway....

    Gary

  5. #5
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA Document Code Sheet

    OK, I figured out that techniques in Excel that (intuitively) should work in Word also, don't carry over so well. Here's what I discovered.

    In Excel, it can be handy to generate a toolbar on the fly, so that you can make the toolbar context-sensitive with respect to which sheet is active. You run the code from every Worksheet_Activate event block, and from the Workbook_Open event block. You can make it even nicer by deleting the toolbar in the Workbook_Deactivate, Workbook_BeforeClose, events, so that the toolbar is only visible when the workbook to which it applies is active. This whole approach is more robust than simply attaching the toolbar to the workbook (because the user could delete it and not know what to do after that). It works quite nicely!

    In Word, since there are no analogues to different sheets, the toolbar contents do not have to be context-sensitive. By putting appropriate code in the ThisDocument events (open, new and close) to build the toolbar from scratch (and delete it when closing), Word thinks that the template itself is changing when you create a new document with the template; when you close the resulting Document1, even if you didn't change anything in it at all, you are prompted, "Do you want to save changes to Such and Such?" In this case, Such and Such is the DOT you used to create Document1. Depending on the name you gave to Such and Such, it may not be obvious what the question means (does it mean the document you created, or the template used to create it...?).

    Anyway, I finally resorted to simply attaching the toolbar to the template itself, getting rid of the event code, and the code for generating the toolbar on the fly. No more mysterious messages. One pain is, Word doesn't seem to have any obvious way to attach an existing toolbar to the template; I had to rename it, create a new one from scratch (at which point, you tell it which template you want to save it in), and drag all the buttons from the original to the new empty toolbar.

    There is another painful aspect, which is, custom toolbars are stored in the template only, not in documents created from the template (this is different from Excel). This means, if the user uses the template to create a Document1, the attached toolbar appears; if they delete the toolbar, it is deleted from the template, not from the Document1 (this is different from Excel). At least, the user is asked (when they close Word or Document1), if they would like to save changes (to the template). If they don't understand why they are being asked, and they choose Yes, they must get a new copy of the template from the originator, or rebuild the toolbar from scratch. Ow!

    Hope someone else can benefit from this experience! Thanks for the help!

  6. #6
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Oregon, USA
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA Document Code Sheet

    <snip>
    There is another painful aspect, which is, custom toolbars are stored in the template only, not in documents created from the template (this is different from Excel). This means, if the user uses the template to create a Document1, the attached toolbar appears; if they delete the toolbar, it is deleted from the template, not from the Document1 (this is different from Excel). At least, the user is asked (when they close Word or Document1), if they would like to save changes (to the template). If they don't understand why they are being asked, and they choose Yes, they must get a new copy of the template from the originator, or rebuild the toolbar from scratch. Ow!
    </snip>

    Hi Jim,

    What about putting the template in a read-only folder?

    Chris

  7. #7
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Silicon Valley, California, USA
    Posts
    273
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA Document Code Sheet

    Yeah, that might work. For my situation, our group is small and in the same room, so it's not critical. I'm just agonizing about differences with Excel, that's all.

    Thanks for the suggestion!

Posting Permissions

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