docx
    Preparing search index...

    Function createMacros

    • 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 as Document_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 as ChrW(&H4E2D) for "中", or two for a character beyond U+FFFF, such as ChrW(&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.

      Parameters

      Returns DocumentMacros & { events: readonly DocumentMacroEvent[]; project: Uint8Array }

      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")] }],
      });

      If a module's name isn't a VBA name of up to 31 characters, or is taken: by another module, ignoring case, or by ThisDocument, the project, Project, or a library it uses, VBA, Word, stdole, Normal or Office

      If code has a character Windows-1252 doesn't