How to Generate a Word Document from Code
Docentric AX introduces new APIs that enable varios new scenarios:
- Generate Word document from template as a binary (to a container).
Generated document data can be used afterwards, e.g. sent as the attachment of an email. - Generate Word document from template on both client and server without attaching it to the Primary table record.
- Generate Word document from template on both client and server and attach it to the Primary table record.
- In contrast to the standard Word documents, a possibility to run on server side enables also batch scenarios.
- You can specify a run-time language as well which is important for multilingual documents.
(Docentric AX enables use of labels on Word templates)
Take a look at the following job:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
static void generateWordDocumentFromCode(Args _args) { PurchTable purchTable; DocuTemplate docuTemplate; container generatedDocumentContainer; Filename outputFilename; select firstOnly docuTemplate where docuTemplate.PrimaryTable == tableStr(PurchTable); select firstOnly purchTable; // 1) -- Generate Word document from template as a binary (to a container). // Generated document data can be afterward used in different scenarios, e.g. sent as a attachment of an email. // The generateDocumentToContainer() method runs on both client and server side. This means that it can be used // in batch scenarios as well. generatedDocumentContainer = DocMailMergeManager::generateDocumentToContainer(docuTemplate, purchTable.RecId); DocEmailMngHelper::sendEmailOnServer('info@docentric.com', 'Docentric Info', 'tom.cruise@gmail.com', '', '', 'Subject', 'Body', false, eMailPriority::Normal, 'PurchOrder.docx', generatedDocumentContainer); // 2) -- Generate Word document from template on client side. // Generate a document from the template and save it to a temporary location on client. outputFilename = DocMailMergeManager::generateDocumentToFileOnClient(docuTemplate, purchTable.RecId); info(outputFilename); // Generate a document from the template and save it to a specifed location on client. DocMailMergeManager::generateDocumentToFileOnClient( docuTemplate, purchTable.RecId, @"Z:\Purchase order.docx", false, true); // Generate a document from the template, save it to a specified location on client and open it afterwards. // Use the provided language as a run-time language (important if this is a multilingual document). DocMailMergeManager::generateDocumentToFileOnClient( docuTemplate, purchTable.RecId, @"Z:\Purchase order.docx", true, false, 'de'); // 3) -- Generate Word document from template on server side. Useful for scenarios in batch. // Generate a document from the template and save it to a temporary location on server. outputFilename = DocMailMergeManager::generateDocumentToFileOnServer(docuTemplate, purchTable.RecId); info(outputFilename); // Generate a document from the template, save it to a specified location on server and use // the provided language as a run-time language (important if this is a multilingual document). DocMailMergeManager::generateDocumentToFileOnServer( docuTemplate, purchTable.RecId, @"Z:\Purchase order.docx", true, 'de'); // 4) -- Generate Word document from template and attach it to the context record of the Primary table. // The generateAndAttachDocument() method runs on both client and server side. This means that it can be used // in batch scenarios as well. DocMailMergeManager::generateAndAttachDocument(docuTemplate, purchTable); // Generate a document from the template, attach it to the provided purchTable record and use // the provided language as a run-time language (important if this is a multilingual document). DocMailMergeManager::generateAndAttachDocument(docuTemplate, purchTable, true, 'de'); } |
See also
How to Email and Print Word Documents >>
How to Use Labels with Word Documents >>
Download Examples for Word Documents >>