# Macros

Source: https://mroffice.ai/en/developers/embed/macros

A macro is JavaScript saved with a document and run on demand, or when the document opens. It is the body of an async function with the object model in scope: editor, word, excel, powerpoint, pdf, doc (editor.document) and console. View > Macros in each editor lists the document's macros, edits them in a code editor and runs them.

A macro:

```js
const hits = await word.search("TODO");
await word.setTextFormat({ text: "TODO", all: true }, { highlight: "#ffff00", bold: true });
console.log(`${hits.length} marked`);

const { address } = await excel.getUsedRange();
if (address) await excel.setFormat(address, { fontName: "Calibri", fontSize: 11 });
```

- A macro runs in a Web Worker inside a sandboxed frame without any network access. Every change it makes is a call checked against the session and one Undo step; a view session's macros only read.
- A run is stopped after two minutes or 5,000 calls, and when the person presses Stop.
- Macros are saved with the document: in MR Office, and inside the .docx, .xlsx or .pptx it exports (a custom XML part, which Word, Excel and PowerPoint keep), so they come back when the file is opened again. Run when the document opens runs a macro once each time the document is opened, after its macros are enabled.

### Macro security

- Macros written by the person themselves run when they ask.
- Macros someone else wrote last stay off until the person enables macros for that document, in the bar the Macros panel shows; they are off again the next time the document is opened in a new browser session.
- ui.features.macros: false hides macros in an embedded editor and runs none.

> MR Office does not run VBA, as ONLYOFFICE does not. The VBA project of a .docm or .xlsm file is kept in the file untouched and is saved back with it.
