Open word template document using mlreportgen.dom API

11 views (last 30 days)
How can I open a word template document (.dotx)?
I can see the options and commands to create new template document but not to open the existing document

Answers (2)

Ayush
Ayush on 19 Jul 2024
Hey Azhar,
You can open an existing Word template document (.dotx) using the mlreportgen.dom API in MATLAB by using the Document class. It allows you to create, open, and manipulate Word documents.Here's how you can achieve this:
  1. Load the mlreportgen.dom package: Ensure you have the mlreportgen.dom package available. This package is part of MATLAB Report Generator. You might need to install it if you haven't already.
  2. Open the Existing Template: Use the Document class to open the existing .dotx file.
  3. Manipulate the Document: Add content or manipulate the document as needed.
  4. Close and Save the Document: Save the changes to a new document or overwrite the existing one.
Code:
import mlreportgen.dom.*;
% Specify the path to the existing .dotx template
templatePath = 'path_to_your_template.dotx';
% Create a Document object using the existing template
doc = Document('MyDocument', 'docx', templatePath);
% Open the document
open(doc);
% Add content to the document
p = Paragraph('This is a paragraph added to the document.');
append(doc, p);
% Close and save the document
close(doc);
% Optionally, view the document
rptview(doc.OutputPath);
For more information on Document class , rptgen in MATLAB. Refer to the following MathWorks documentation:
  1. https://www.mathworks.com/help/rptgen/
  2. https://www.mathworks.com/help/rptgen/ug/mlreportgen.dom.document-class.html
Hope this helps!
Regards
  1 Comment
Azhar
Azhar on 19 Jul 2024
Hi @Ayush thanks for the quick response, the example what you shares creates a new document based on the template. What I am looking for is to open/load existing template to read/edit its properties.

Sign in to comment.


Zinea
Zinea on 19 Jul 2024
Hi Azhar,
The ‘mlreportgen.dom’ API does not directly support opening and modifying an existing ‘.dotx’ file. This API is designed primarily for creating new documents, either from scratch or based on a template, and then adding content programmatically.
To modify an existing ‘.dotx’ file, you can follow one of the approaches listed below:
1.You can create a new document based on a template and then modify the new document. Refer to the following documentation link for more details:
2. If you are using MATLAB on Windows, you can use ‘ActiveX’ to interact with Microsoft Word and open an existing ‘.dotx’ file named ‘MyTemplate.dotx. Here is a code snippet demonstrating this workflow:
Define the path to your .dotx file
dotxPath = fullfile('path_to_dotx_file', 'MyTemplate.dotx');
% Create an ActiveX server to interact with Microsoft Word
wordApp = actxserver('Word.Application');
% Make the Word application visible
wordApp.Visible = true;
% Open the .dotx file as a template
document = wordApp.Documents.Open(dotxPath);
You can refer to the documentation link for actxserver’ for more details:
Hope it helps!
Best,
Zinea

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!