Event Macros

Liberty Reports can run macros that you record to run in response to specific actions. For example, you can record a macro that is run just before refreshing data so you can carry out your own custom steps that might be needed at that time. These macros are called "Event Macros" and must be given a specific name when you record them so that Liberty Reports will run them.

Steps to Create an Event Macro

Follow these steps to record a macro tied to a Liberty Reports event:

  1. On the Developer tab, click Record Macro (in the Code group).
  2. In the Macro name box, enter one of the macro names from the Event Macros List below. Note that the macro name must match exactly, so using Copy and Paste might be helpful.
  3. Click OK.
  4. Carry out the steps that you want your macro to perform and then click Stop Recording on the Developer tab.
    Note - If you perform an action that triggers the event while you are recording it, then an infinite loop will be started. Liberty Reports detects this scenario and prompts you to cancel the action, but it is best to avoid it altogether.
  5. On the Developer tab, click Stop Recording (in the Code group)

Event Macros List

ActionAssociated Macro Names
Liberty Reports: Save As ValuesBefore_LRSaveAsValues

After_LRSaveAsValues
Liberty Reports: Refresh DataBefore_LRRefreshData

After_LRRefreshData
Excel: New WorkbookLR_OnNewWorkbook
Excel: Saving a workbook templateLR_OnSaveTemplate

Advanced Usage

The default Before and After event macro functionality can be extended by manually editing the recorded macro procedures.

Canceling an Event

All of the events that start with Before_ can be canceled, preventing the action from taking place. To accomplish this, change the macro from a Sub to a Function that returns a Long. To cancel the action, assign a non-zero return value to the function. For example:

Public Function Before_LRRefreshData() As Long

  'Cancel the refresh if there is no job number
  If IsEmpty(Range("A1")) Then
    Call MsgBox("You must enter a job number before refreshing the workbook.", _
                 vbInformation)
    Before_LRRefreshData = -1
    Exit Function
  End If

End Function

Using Return Values

All of the events that start with After_ can be modified to indicate if the action was successful. To accomplish this, add a Long parameter to the Sub. Liberty Reports will pass the return code from the associated action via this parameter. Zero indicates success and other values indicate cancellation or errors. For example:

Public Sub After_LRRefreshData(ByVal lvOperationResult As Long)
  
  'Don't do anything if the refresh was canceled or did not complete due to an error
  If lvOperationResult <> 0 Then
    Exit Sub
  End If
  
  'Run the code to perform post-refresh steps here
  
End Sub