Results 1 to 10 of 10
  1. #1
    garyzim
    Guest

    Executing a command macro on document open

    Hello loungers,
    I have a question:
    The GoBack command (default keystroke it shift-F5) lets you return to the location of the last edit in a document, and is really helpful when you open a document you haven't worked on in some time. It helps you remember where you left off.

    I'd like to modify Word's (97, SR2) behavior to execute this command automatically whenever a document is opened, either from the File>Open command or just by double-clicking the doc file.

    I tried to do this by writing a simple autoexec macro, but it doesn't work because, if you double click a file to open it, apparently the macro is run before the document window is open, so an error results.

    Anyone know how I can accomplish this?

    Thanks very much in advance,
    -- garyZ

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

    Re: Executing a command macro on document open

    Hi GaryZ,

    Try putting the macro into a Document_Open procedure in the ThisDocument class module for the attached template, or Normal.dot, whichever is applicable.

    The various auto and document event macros have a hierarchy in terms of when they fire (can't cite you the exact order, though). It may be a matter of locating your code in the right one to get things to go in the order you need.

    Hope this helps,
    GaryF

  3. #3
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Executing a command macro on document open

    Record a macro named AutoOpen and store it in Normal.dot or in your document's template or in the document itself.
    Charles Kyle Kenyon
    Madison, Wisconsin

  4. #4
    garyzim
    Guest

    Re: Executing a command macro on document open

    Hi Charles,

    Thanks for the suggestion! I haven't actually tried it out, though, because Gary F's suggestion worked for me. He suggested I write a document_open() method for This Document. The tricky part is finding where to stick it.

    MS and VB make that less than obvious (to me), though, because when I opened the VB editor and found the This Document item under the NormalMicrosoft Word Objects, I couldn't find a way to add any functions to it.

    What I needed to do (GaryF told me) was to RIGHT CLICK This Document to reveal "View Code" on the popup menu. If you click that, you get to the place you can write procedures that are applied to all Normal documents.

    I simply added this:

    Private Sub Document_Open()

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Executing a command macro on document open

    Hi Charles:

    I could be wrong (won't be the first time[img]/w3timages/icons/grin.gif[/img]), but I think it will only work in your document. If you put an AutoOpen macro in a template, it only has an effect if you open the template, not a document based on that template.

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

    Re: Executing a command macro on document open

    Hi Phil,

    It was right the first time 'round[img]/w3timages/icons/blush.gif[/img] - the code will run when you open a document based on that template, whether it is Normal.dot or an attached template.

    BTW this sometimes adds special problems if you have AutoOpen or AutoClose (or Document_Open or Document_Close) procedures in your templates - if they're meant to do something to the document, when you open a document based on the template, but they also run when you open the template (which may change the template in a way you don't want)!

    So sometimes you need to add code testing to see whether the file which is opening, is a document or template, in front of the procedure you want to run upon open or close.

    Gary

  7. #7
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Executing a command macro on document open

    Phil,

    Welcome to the club! (I've been wrong myself many a time and hopefully will be again, that's how we learn.)[img]/w3timages/icons/doh.gif[/img]
    If you put an AutoOpen macro in Normal.dot, it will run everytime any document (or template) is opened.
    If you put an AutoOpen macro in a template, it will run anytime a document based on the template is opened (so long as the template remains attached).
    If you put an AutoOpen macro in a global template, it will run only when the template is opened.

    The same is true for AutoClose macros.
    However, AutoExec macros will run anytime the template/document containing the macro is loaded. (For Normal.dot and globals this is when Word is started. For other templates it is when the template or a document based on the template is opened. For documents, it is when the document is opened. (AutoExec macros can be in Normal.dot, global.dot, attached.dot and document.doc and *all of them will run!* Normally when you have macros with the same name in multiple active places, only the macro closest to the document runs. For AutoOpen macros, if you had an AutoOpen macro in all four places only the macro in the document would run.)
    Charles Kyle Kenyon
    Madison, Wisconsin

  8. #8
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Executing a command macro on document open

    See! It didn't take that long before I was wrong again. AutoExec macros won't run in a document or document template, only in Normal.dot and other globals.
    Charles Kyle Kenyon
    Madison, Wisconsin

  9. #9
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Executing a command macro on document open

    Hi Chas & Gary:

    OK. Thanks for straightening me out. I don't know why I was thinking that AutoOpen had to be put in a document itself to work, when the document was opened.[img]/w3timages/icons/blush.gif[/img] [img]/w3timages/icons/blush.gif[/img] If that were true, then there wouldn't have been any AutoOpen or AutoClose macros back with Word 6 & 7, because documents then couldn't contain macros, only templates.

    And Chas, while you went to far with AutoExec macros in your exuberance, you're right on the money with everything else.

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

    Re: Executing a command macro on document open

    Hi,

    In case it's of interest, here's the result of the following experiment:

    Create a new document based on Normal.dot.
    In both the document and Normal.dot, add the following procedures:
    AutoOpen
    AutoClose
    Document_Open
    Document_Close
    (each one runs one line of code, along the lines of:
    MsgBox "This is running from the AutoOpen in Normal.dot" etc.

    When you then open the document, the following run, in the following order:
    AutoOpen (in the document)
    Document_Open (in Normal.dot)
    Document_Open (in the document)

    And when you close the document, the following run, in the following order:
    AutoClose (in the document)
    Document_Close (in Normal.dot)
    Document_Close (in the document)

    So while only one "Auto" macro will run (the one "nearest" the document), both "Document_" procedures will run.
    And the order in which these run is not necessarily what one would expect.

    Gary

Posting Permissions

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