Clear Filters
Clear Filters

How do I generate a multi-page word document using report generator

50 views (last 30 days)
I am trying to generate a multi page Word document using MS word templates, filling in holes etc.
I am able to generate a single page document as desired by adding holes in the MS word template. Is any way I can design my template and code so that I can loop and each iteration of the loop generates a new page in the word document, each time filling in the same holes with updated values?
  2 Comments
Chris
Chris on 24 May 2022
I solved it but it wasn't an elegant solution. I used a template to write out many word documents whose filename corresponded to the page order. I then used an external program that combines PDFs from the command line to package it into a single PDF. There may be a better way to do it, this is what I came up with.

Sign in to comment.

Answers (1)

Sanchari
Sanchari on 5 Jan 2024
Hello Chris,
I understand that you want to generate a multi-page MS-Word document using the MATLAB Report Generator.
You can do so by filling in placeholders (holes) with updated values on each iteration of a loop. Assuming that you are using a Windows platform, you can use MATLAB's ‘actxserver’ to create an ActiveX server that interfaces with Microsoft Word, allowing you to control Word from MATLAB.
Here's a step-by-step approach to achieving this multi-page document using MATLAB Report Generator:
1. Design the Word Template:
  • Create a Word document that will serve as your template.
  • Insert placeholders where you want to insert the updated values.
  • Placeholders can be simple text that you will search for and replace in your MATLAB code (e.g., <placeholder1>, <placeholder2>, etc.).
2. Write MATLAB Code:
  • Start by creating a Word ActiveX server and opening your template document.
  • Use a loop to iterate over the data you want to insert into the document.
  • For each iteration, search for the placeholders and replace them with the new values.
  • Insert a page break at the end of each iteration, except for the last one.
Please try this simplified example of MATLAB code that demonstrates the process:
% Define the path to your Word template
templatePath = 'C:\path\to\your\template.dotx';
outputPath = 'C:\path\to\your\output.docx';
% Start an ActiveX server for Word
wordApp = actxserver('Word.Application');
wordApp.Visible = true; % Set to true to see the Word application
% Open the template
document = wordApp.Documents.Open(templatePath);
% Assume 'data' is a struct array or cell array with the data for each page
for i = 1:length(data)
% If not the first page, create a new page by inserting a page break
if i ~= 1
wordApp.Selection.InsertBreak; % This inserts a page break
% Insert the template content again for a new page
wordApp.Selection.InsertFile(templatePath);
end
% Find and replace placeholders in the document
% Assuming 'placeholders' is a cell array of placeholder strings and
% 'data' contains the corresponding replacement values for each page
for j = 1:length(placeholders)
% Set the range for the Find operation
findRange = wordApp.ActiveDocument.Content;
findRange.Find.ClearFormatting;
findRange.Find.Text = placeholders{j};
findRange.Find.Replacement.ClearFormatting;
findRange.Find.Replacement.Text = data(i).(placeholders{j});
% Execute the Find and Replace operation
findRange.Find.Execute(Replace=wordApp.wdReplaceAll);
end
end
% Save the document
document.SaveAs2(outputPath);
% Close Word
document.Close;
wordApp.Quit;
% Release the COM object
delete(wordApp);
In this example, data is a cell array with the values to be inserted into the document. The placeholders’ <placeholder1>’, ‘<placeholder2>’, etc., are replaced with the actual values from the data array. The ‘InsertBreak(7)’ method is used to insert a page break. Please adjust the ‘findText.Text’ and ‘wordApp.Selection.TypeText’ lines to match your actual placeholders and data structure and update the file paths to your template and output document locations.
Hope this information is helpful to you!

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!