Contents
Index 🔎︎

# Document Event-Driven Macros

This section describes how to assign scripts to application, document or form events.

You can automatically execute a macro when a specified software event occurs by assigning the desired macro to the event. The following table provides an overview of document events and at what point an assigned macro is executed.

Event | An assigned macro is executed... | routine
Start Application | ...after a LibreOffice application is started. | OnStartApp
Close Application | ...before a LibreOffice application is terminated. | OnCloseApp
Document created | ...New document created with File - New or with the New icon. Note that this event also fires when Basic IDE opens. | OnCreate
New Document | ...after a new document is created with File - New or with the New icon. | OnNew
Document loading finished | ...before a document is opened with File - Open or with the Open icon. | OnLoadFinished
Open Document | ...after a document is opened with File - Open or with the Open icon. | OnLoad
Document is going to be closed | ...before a document is closed. | OnPrepareUnload
Document closed | ...after a document was closed. Note that the "Save Document" event may also occur when the document is saved before closing. | OnUnload
-no UI- |  | OnLayoutFinished
View created | Document is displayed. Note that this event also happens when a document is duplicated. | OnViewCreated
View is going to be closed | Document layout is getting removed. | OnPrepareViewClosing
View closed | Document layout is cleared prior to the document closure. | OnViewClosed
Activate Document | ...after a document is brought to the foreground. | OnFocus
Deactivate Document | ...after another document is brought to the foreground. | OnUnfocus
Save Document | ...before a document is saved with File - Save or the Save icon, provided that a document name has already been specified. | OnSaveAs
Document has been saved | ...after a document is saved with File - Save or the Save icon, provided that a document name has already been specified. | OnSaveDone
Saving of document failed | Document could not be saved. | OnSaveFailed
Save Document As | ...before a document is saved under a specified name (with File - Save As, or with File - Save or the Save icon, if a document name has not yet been specified). | OnSaveAs
Document has been saved as | ... after a document was saved under a specified name (with File - Save As, or with File - Save or with the Save icon, if a document name has not yet been specified). | OnSaveAsDone
'Save As' has failed | Document could not be saved. | OnSaveAsFailed
-no UI- | When the document disk location has changed, for example after a File - Save As... action. | OnStorageChanged
Storing or exporting copy of document | ...before a document is saved with File - Save a Copy, File - Export, File - Export as PDF or the Save icons. | OnCopyTo
Document copy has been created | ...after a document is saved with File - Save a Copy, File - Export, File - Export as PDF or the Save icons. | OnCopyToDone
Creating of document copy failed | Document could not be copied or exported. | OnCopyToFailed
Print document | ...after the Print dialog is closed, but before the actual print process begins. This event occurs for each copy printed. | OnPrint
-no UI- | ...after document security settings have changed. | OnModeChanged
'Modified' status was changed | Modified state of a document has changed. | OnModifyChanged
Document title changed | When the document title gets updated. | OnTitleChanged
Loaded a sub component | ...after a database form has been opened. | OnSubComponentOpened
Closed a sub component | ...after a database form has been closed. | OnSubComponentClosed
Printing of form letters started | ...before printing form letters using File - Print or Tools - Mail Merge Wizard menus. | OnMailMerge
Printing of form letters finished | ...after printing of form letters using File - Print or Tools - Mail Merge Wizard menus. | OnMailMergeFinished
Printing of form fields started | ...before printing form fields. | OnFieldMerge
Printing of form fields finished | ...after printing form fields. | OnFieldMergeFinished
Change of the page count | When the page count changes. | OnPageCountChanged

Most events relate to documents, except OnStartApp, OnCloseApp, OnCreate and OnLoadFinished that occur at application level. OnSubComponentOpened, and OnSubComponentClosed events are fired by database's forms.

Writer documents are triggering those specific events: OnLayoutFinished, OnMailMerge, OnMailMergeFinished, OnFieldMerge, OnFieldMergeFinished and OnPageCountChanged.

## Assigning a Macro to an Event

1. Choose Tools - Customize and click the Events tab.

2. Select whether you want the assignment to be globally valid or just valid in the current document in the Save In listbox.

3. Select the event from the Event list.

4. Click Macro and select the macro to be assigned to the selected event.

5. Click OK to assign the macro.

6. Click OK to close the dialogue.

## Removing the Assignment of a Macro to an Event

1. Choose Tools - Customize and click the Events tab.

2. Select whether you want to remove a global assignment or an assignment that is just valid in the current document by selecting the option in the Save In listbox.

3. Select the event that contains the assignment to be removed from the Event list.

4. Click Remove.

5. Click OK to close the dialogue.

Related Topics

In addition to assigning macros to events, one can monitor events triggered in LibreOffice documents.