The document's macros, for its macros: the VBA project, and the events of the document it handles
const doc = new Document({
macros: createMacros({
thisDocument: 'Private Sub Document_Open()\n MsgBox "Hello"\nEnd Sub',
modules: [{ name: "Formatting", code: "Sub MakeBold()\n Selection.Font.Bold = True\nEnd Sub" }],
}),
sections: [{ children: [new Paragraph("Hello World")] }],
});
Writes a document's VBA macros from their code, for the document's
macros: the VBA project, and the events of the document that ThisDocument's code handles, such asDocument_Open. The document is then a macro-enabled document, which Word opens from a .docm file. Word asks whether to enable its macros, as for any document with macros from elsewhere.VBA keeps code in Windows-1252, the code page of Word in English and the other Western European languages, so a character it doesn't have goes in a string as
ChrW, such asChrW(&H4E2D)for "中", or two for a character beyond U+FFFF, such asChrW(&HD83D) & ChrW(&HDE00)for "😀".Word compiles the code when it first runs a macro, so a mistake in it shows then, in the VBA editor.