|
Automatically insert the current
date as unchanging text in a letterhead or other template
NOTE: If you are editing an existing
document that has lots of date codes that cause those
dates to change each time you open the document, and want to
replace all of them with the current date (as text strings,
not codes), see the DATEREPL
macro in the Library. It was designed to be played from a toolbar
button or keystroke combination whenever you need to replace
changeable dates with static dates in an existing document. |
A user on one of the Corel newsgroups
asked:
"I'm trying to create a
letter macro that automatically inserts the current date. Since
I work in a law office the date on the letter needs to remain
constant - it must be printed as text not a date code that changes
from day to day. Anyone have any ideas around this?"
One method:
This requires a little different
procedure from what would be required to insert the date as a
code. (See the alternative method below,
which uses replaceable date codes.) Nonetheless, it is simple
and straightforward, and should be easy for even a novice user.
Since you need the current date
(which, by the way, is taken from your system's clock, so you
want to be sure the clock is set properly!) you can use a simple
macro embedded in the template itself to automatically insert
it as unchanging ("static") text in every new document
you open based on that template.
Since these dates will be inserted
as text characters -- just as you would manually type
them in -- this is done just once by the macro, when you load
a new document from the template with File> New from Project
(or File> New in WP8 and earlier).
Here's how to do it:
- Open your letter or other template
(i.e., either edit it or create a new one using the Options button
in the File>New from Project dialog window).
- The first -- and very easy --
step is to record a template macro. Click on Tools>Template
Macro>Record. A dialog will pop up at this point. Enter a
name for your new macro (e.g., "Insert date as text")
in the top field, and click the Record button. (Note that a Macro
Toolbar is now displayed on screen. You'll use this later to
stop the macro recording.) At this point, your keystrokes
and menu selections are now being recorded.
- Place the cursor where the date
should go, if it is not already there.
- TIP: You can hit the <Enter> key, <Spacebar>,
etc., or use the Format>Typesetting>Advance feature to
specify an exact location. (Remember, your actions are being
recorded, so it might be a good idea to list these extra customization
steps and settings in advance so that you'll produce a "cleaner"
template macro.)
- Using the main WP menu, click
Insert>Date/Time. Select the date format you need -- but be
sure that "Keep inserted date current" is NOT checked
-- then click Insert. The date (as text) should now be displayed
on screen at the cursor location. Repeat to insert other dates,
such as inside a header or a footer.
- Tip: At this point you might want to use
the <Enter> key to move down a few lines below the date
to where the recipient's address will eventually be typed.
- Click the leftmost button --
the one with a small black square on it -- on the Macro Toolbar
to stop the macro recording. Now, before you save your work you
need to do several things.
- 1. You need to remove
the new text date, hard returns [HRt], and any [Adv] codes you
just entered from the main screen (use the Reveal Codes window
to help locate them; or use Edit>Undo to remove these items
from the template). Reason: You want to save your newly edited
template without this new stuff, and have the macro do
the work of inserting the text date, hard returns, etc., later,
whenever you open a new document based on this template. If you
don't remove the items you just entered in the template, you'll
get two dates, etc., in new documents. So, remove just those
things from the letter template document that were entered during
the macro recording process above. (Note that this does not remove
the macro you just recorded, only the results of your actions
during the creation of the macro. The macro code will be saved
in the template file itself, which is what you want to do.)
- 2. You need to "associate"
this template macro with something called the "Post New
trigger," so that it will play
as soon as you open a new document (hence, Post New) based
on the template. Here's how:
- While the template is being
edited, click the Associate button on the template toolbar. In
the dialog that pops up, click the Triggers radio button. Then
select (click) "Post New" in the left pane, and choose
the new template macro's name from the Macros drop list. Click
OK to close the Associate dialog.
- Important: If you see something called "<dofiller>"
already associated with the Post New trigger, stop
now and read Footnote 1. Otherwise, continue.
- 3. If this was a pre-existing
template, it might have been set up with one or more dates inserted
as codes so that they always display (and print) the current
date. You probably will want to remove these [Date] codes unless
you really want them to always show the current date when you
open the document in the future. Open Reveal Codes and examine
the template; remove any unwanted [Date] codes with the <Delete>
key or by dragging the codes from the Reveal Codes window. (Be
sure to check for such codes in any header or footer by simply
clicking your mouse inside these structures.)
- Finally, save your modified
template with File>Save, and exit from the template with File>Close,
or click the rightmost button on the template bar.
- Test your changes. Open a new
document based on this template; the date (as text, not as a
[Date] code) should be automatically entered. If you added hard
returns or an Advance code, the cursor should move down a few
lines, waiting for the recipient's name and address to be typed.
An alternative
method:
- The advantage this method has
over the method above is that it might make the template modification
process easier for you, especially in a complex template. It
uses an existing macro that will automatically change any existing
date codes to text dates when you open a new document based on
that template. This saves you the minor effort of recording a
new template macro, removing changes to the template, etc., as
described above. It also makes it slightly easier to insert dates
where you need them, since you do this in advance by inserting
date codes. A disadvantage is that it doesn't easily let you
add extra elements (hard returns, etc.) to the template, since
it uses an existing macro (DATEREPL)
from this site, not your own macro.
- Create the new template (or
edit the old one) and use [Date] codes wherever you need them.
(This is the way many WP Projects work when they need to display
a date.) That is, when you insert dates in the body text, headers,
etc., with Insert>Date/Time, be sure to check the box "Keep
the inserted date current," which inserts the date as a
code rather than as a string of characters. To save typing time
in creating the new template, you could insert an entire existing
document into the new template with File>Insert.
- Then use the DATEREPL
macro as a template macro, associated with the Post New trigger.
This way, you can have code dates in body text, headers, footers,
etc., and the macro will find all of them all and replace them
with today's date as text. Here's how:
- Copy DATEREPL into the new template
(first be sure DATEREPL.WCM is in your default macro directory).
From the template toolbar click Copy/Remove Object. In the next
window, under "Object type:" select "Macros on
disk." Click the button on the "Source:" field
to select the macro from the directory. Then click the Copy>>
button, then click Close. The macro is now embedded in the template
as a template macro.
- Next, click Associate from the
template property bar, then click the "Triggers" radio
button.
- In the "Item to Associate"
list, select "Post New." (By associating the macro
with the Post new trigger, the macro will automatically play
immediately whenever a new dcoument is opened based on that template.)
- Important: If you see something called "<dofiller>"
already associated with the Post New trigger, stop
now and read Footnote 1. Otherwise, continue.
- In the "Macros" drop
list, select the macro (Daterepl), then click OK.
- Save the template. If it is
a new template, give the template a description and a filename
in the Save Template dialog (see the Help button for more information),
and select a Category for the new template (such as Custom WP
Templates).
Note that the only part of DATEREPL that is important
for the operation of this template macro is shown below. You
could edit out all other lines in that macro before copying it
into the template:
PosDocVeryTop
MatchSelection
OnNotFound (END@)
While(1) // Enter [Date] in
the SearchString command with //
the Codes button on the macro toolbar:
SearchString ("[Date]")
SearchNext (Extended!)
DateText
EndWhile
Label(END@)
TIP:
For a more complete treatment
on letterheads and other automated documents, see LETTERHD
in the Library, which contains sample templates. |
|
Footnote 1
The <dofiller> is the oddly
named built-in function (a macro, really) that pops up the Template
Information dialog when you open a document based on a template
that uses this function (not all templates use it). If you see
a dialog with fill-in fields (also called "prompts")
when you open such a document, it's because the template uses
<dofiller> to display the dialog and insert the material
in each field in various locations in the document.
Unfortunately, only one macro
can be associated with any given template trigger.
You should not blindly replace <dofiller> with your own
macro or any other macro, or you most likely will change the
function of the template in a way you do not expect or do not
want.
You can work around this problem,
but you should learn about the triggering function and how to
code a macro to incorporate both the function of <dofiller>
and the function of date-code replacement, such as described
on this page and coded in the DATEREPL
macro. Typically, this simply means adding a single command (TemplateFill)
to the beginning of the macro that does some custom work -- such
as the replacement of date codes -- and then associating that
macro with the Post New trigger. This comes under the topic of
"Automating WordPerfect Templates," which is comprehensively
described here.
[Although the linked document (a PDF file) is lengthy, it can
pay great dividends in productivity -- not to mention reduced
frustration -- in the future, especially if you are tasked with
automating WordPerfect.] |