Home | Tips | Library | Other Authors | Other WP Sites | Writer's Links | Contact | Site Map | Donate

Barry MacDonnell's
Toolbox for WordPerfect

Macros, tips, and templates for Corel® WordPerfect® for Windows®
© Copyright 1996-2021 by Barry MacDonnell. All Rights Reserved.

Page updated Oct 11, 2013

WordPerfect Tips
Main tips page | Browse more tips
"Trigger" a macro from inside a template to play automatically at specific times (events), such as when you open a new document or before you print a document

WordPerfect 9's online Help (F1, Index tab, "Triggering macros from templates") says this:

You can play a macro as you use a template. To do this, record a template macro, then associate the macro with a trigger to start the macro.

For example, if you use a template to create letters, you can set a PRE PRINT trigger in the template that runs a spell-check macro. The macro then lets you spell-check your letters just before they print.

You can use the macro triggers in the following table:

POST CLOSE Runs in the active window after you close a window.
POST NEW Runs after you open a window by clicking File, New.
POST OPEN Runs after you open a window by clicking File, Open.
POST PRINT Runs after you send a print job to the printer.
POST STARTUP Runs when you start WordPerfect. The macro must be associated with the default template.
POST SWITCH DOC Runs in the active window after you switch from another window.
POST TABLES Runs after you create a table structure in a document, but before you enter data in the table.
PRE CLOSE Runs before you close the active window by clicking File, Close.
PRE NEW Runs in the active window before you open another window by clicking File, New.
PRE OPEN Runs in the active window before you open another window by clicking File, Open.
PRE PRINT Runs after you click File, Print, but before the print job is generated.
PRE SWITCH DOC Runs in the active window before you switch to another window.
PRE TABLES Runs after you insert a table by clicking Insert, Table, but before the table structure is created in the current window.

Note that you can have only one macro associated with a particular trigger event.

The two most common triggers used with simple template macros are Post New (i.e., play on creating a new document based on the template) and Pre Print (i.e., play at print time).

In addition to recording a template macro (i.e., recorded while editing the template) you can use a previously recorded macro stored on your disk. Here's how.

Assuming the macro is located in your default macros folder as shown in WordPerfect's Tools, Settings Files, Merge/Macro -

  • Edit the template on which the document will be based by (usually) clicking on File, New from Project -- then right-clicking the template's name and choosing Edit WP Template. (The default template on which all new documents are based is (blandly) named "Create a new document.") The template document should now be on screen.
    • [This is the point at which you could record a template macro with Tools, Template Macro, Record. However, here we are going to use a macro already stored on disk.]
  • On the Template Property Bar, click Copy/Remove Object. In the dialog that appears, click the down arrow in the Object Type window and select "Macros on Disk." In the Source field, click the browse button and select the macro by name. Click the Copy>> button to copy the macro to the template, then click Close.
  • You should now be back in the template document. Now we'll "trigger" this macro to play automatically. For example, we can make it play immediately after you choose File, Print... (but before the print job starts). Here's how:
  • Click the Associate button on the Template Property Bar. Next, click the Triggers radio button, then select "Pre Print" in the left window. In the Macros drop list, choose the name of the macro and click OK.
  • Save the template with File, Save. 
  • Test the operation of the macro by loading a new document based on the template, typing some text into it, and then printing it with File, Print. 
  • For more detailed information about triggering macros from a template, see "Automating templates" -- especially the PDF document, "Automating WordPerfect Templates." (But please review the notes below before leaving this page.)

Important notes

  • Older documents that were based on the template that were created before you installed the template macro (or were created  under some other template) will not automatically play the macro. The macro didn't exist inside those templates at the time the documents were created, so you may also want to assign a disk version of the macro to a toolbar button or keystroke combination. You can then manually play it as needed.

  • Ordinarily, only template macros can be associated with a trigger. Macros on disk that are not already part of that template cannot be called (i.e., started) by a template trigger, but they can be copied into the template first and then associated with a trigger, as explained above. However, if a disk macro already has been included on a toolbar (or menu, or associated with a keystroke) in that template, then it will be available to associate with a trigger.

  • If you want to use the POST NEW trigger and notice something already associated with it called "<dofiller>" here's an explanation (admittedly a bit convoluted):

    • WordPerfect's DoFiller feature is normally activated by Prompt Builder (which can be used when creating an automated template) and then associated with the Post New trigger in the template. DoFiller automatically fills in fields in a template that are linked to your Personal Information.

      But if you use a "startup" template macro associated with Post New -- and since you can have only one template macro associated with a trigger -- you will need the TemplateFill () command to activate DoFiller as well as display the Template Information dialog, if required, to get prompted information from the user.

      You can place the TemplateFill() command anywhere in the "startup" template macro where you want the Template Information dialog to appear, but most often it is used as the first command.

      (Think of TemplateFill() as the macro command equivalent of the DoFiller feature. For more detailed information see Automating WordPerfect Templates (.pdf).)

    • For those who want to know more about automating letterhead templates, see LetterHd in the Library.

  • If you plan to share documents containing template macros with other WordPerfect users, take note of this:

    • If a template containing a template macro is set to be triggered by some event (such as opening a new document based on that template), note that the template macro will only trigger in a document (.WPD) if the template (.WPT) is present, either in the original template folder or in the default (parent) template folder. If WordPerfect cannot locate the template, the template macro will not play even though it is embedded in the document itself, since the trigger specification is stored in the template, not in the resulting document.

    • This can cause problems when sharing such automated documents with other WordPerfect users. Make sure they also have the same template that spawned the document and it is located in the same relative folder as the original. [This might explain why template macros fail to automatically play in some documents if the original template has been renamed, relocated, or removed.]

  • A corollary of the previous note: If you change or add a template macro in a template, all documents that were previously created with that particular template should -- if the template is present -- be able to run the revised (or new) template macro if they are set up to be triggered (or if they are manually played via the Tools menu, a toolbar, or a shortcut key).

  • If a template macro does not run automatically when loading a new document or WordPerfect Project, or when some other expected trigger event occurs, template macros may have been turned off by a user (even if unknowingly).

    • According to the Corel Knowledge Base, "This issue can occur if template macros have been disabled. When opening one of [these automated] files, you will get a dialog box entitled ‘PerfectScript', explaining that this document has a macro, and that macros can contain malicious code. If the ‘Do not show this message again' box is checked, and ‘Yes' is selected, that prompt will not come up again, and the macros will not run. For solutions, see here.

  • The trigger associations for a given template can also be determined with a macro. See Klaus Pfeiffer's macro posted (and downloadable) on WordPerfect Universe here.