Creating And Using Macros

Using Macros.


1. Introduction

Macros are similar to "auto correct" function in Microsoft Word where a small string of text can be expanded into a sentence or even a paragraph of text. The text fields can contain up to 5000 characters.

For example, setting up a macro called "nadx" might expand to "Clinical examination of the chest and abdomen was normal". Once set up, anytime you write nadx in the note field and then press enter, it will expand to the text you have chosen (Clinical examination of the chest and abdomen was normal) in the expression field.

You can examine the list of default macros by clicking on Administration | Lookups select Macros from the Type drop-down box and click Find.

This is a very handy tool to decrease typing time in consultations; it can also be useful to create headings in the notes section of the consult page.

2. Creating Your Macro

Let's set up the example macro from the introduction above.

  1. Click on Administration | Lookups | New, scroll down and click on Macros
  2. You are now presented with a blank form.
    Code: this field contains the actual code you wish to use: the code must consist of alphanumeric characters and @ and _ - hence abc, A4b, a_B, a_4, _abc, @aBc, ab@c are all valid. Take care not to name the code an actual word as it will expand into text inappropriately, for example bid can transform into twice daily when you may just mean bid. Perhaps end the code in "x" to avoid this mishap. In the example above, we've called the code nadx. Alternatively you can use @ or _ as a prefix.

    Name: A descriptive name for your macro. You can search for your macro by this name in the search field at the top of the macro window.

    Expression: Enter the text you wish the code to expand to. You have a 5,000 character limit and can format with leading spaces and paragraphs. The text should be enclosed in double quotes. Actually you can use single quotes BUT there's a "gotcha" if you do: if you use the single quote as an apostrophe in the text (few if any keyboards have a separate apostrophe key), the macro will fail. For example, if you want to have text such as today's temperature, the single quote in today's will cause the macro to fail if the whole of the text is not enclosed in double-quotes.

    In the example below, we've used nadx, NAD, "Clinical examination of the chest and abdomen was normal" in the three fields.
    Click Ok when you're done.

3. Using Your Macro

When writing your notes, simply type in nadx and press Enter

Your macro will be expanded to the text you've specified.

As mentioned above, macros can insert up to 5,000 characters of text: here's an example for a cat spey:

Entering text and you can't remember the macro? Press Alt-m - this will display the available macros so you can click the one required.

4. Trouble-shooting

  1. The most common error is when you haven't enclosed the macro in quotes - it simply doesn't expand when you type in the macro code. Solution is obvious - make sure you have double-quotes at the beginning and end of the macro.
  2. If you created the macro in 1.4 or earlier and have now upgraded to 1.5.1 or higher, you will get the error shown below if you try to save after editing the macro.

    The upgrade from 1.4 to 1.5 added a new field in the Macro lookup display called Description. The problem arises because the upgrade procedure populates the description field with the code-name of the macro.

    As soon as you try to save the macro you are editing, openVPMS tries to expand the macro in Description, and generally can't because of the macro's length, and then returns the error below.

    Solution again is simple: when editing a macro, make sure the Name and Description fields are NOT the same as the Code field.
    An entry in the Description field is not required for the macro to work.

5. Expression Features

The Expression can contain more that just text - it can incorporate the preceding number, it can invoke another macro, and it can perform a lookup.  You can also use the concat function to concatenate things.

$number: If you define a macro, say @hrs to have the expression concat('every ',$number,' hours') then when you enter 6@hrs, it will be replaced by "every 6 hours".  That is, the number preceding the macro code ('@hrs' in this case) can be accessed using the special variable $number. As well as simple integers (like 6 or 10), you can use decimals (like 2.5) or fractions (like 1/4).

Lookups: If you define a macro, say dispensingUnits, to have the expression openvpms:lookup(openvpms:get(.,'product.entity'), 'dispensingUnits') this will be used to look up the dispensing units for the current product. Other useful examples are:
openvpms:lookup(openvpms:get(.,'product.entity'), 'dispensingVerb') and
openvpms:lookup(openvpms:get(.,'product.entity'), 'sellingUnits')

Obviously, you need a understanding of the archetypes to make extensive use of this facility.

Macros in Macros: We can make use of the above two facilities by defining a macro, say @oid with the expression:

  concat($dispensingVerb, ' ', $number, ' ', $dispensingUnits, '(s) Once Daily')

Here we are invoking two other macros, dispensingVerb and dispensingUnits, and also making use of the $number.  Thus 4@oid will expand to something like "Give 4 tablet(s) Once Daily"

Note that invoked macro (eg 'dispensingVerb') MUST have a name that starts with a letter, otherwise the expansion will not work.

Home Page

Created 25 August 2011 (compiled by Yuri Sos based on forum post by Sam Snelling), amended 24 July 2012

Syndicate content