How to get values of pre-filled holes in dotx template

5 Ansichten (letzte 30 Tage)
Marcio Fernando Cobo
Marcio Fernando Cobo am 9 Mär. 2025
Beantwortet: Jack am 9 Mär. 2025
Hi,
I would like to import a template.dotx that contains holes, some being already pre-filled beforehand.
For instance: Imagine that a holeID "ahole1" that contains in the dotx a predefined value x, written when creating the dotx.
In the script, I would like to read the value of "ahole1" prior deciding what to write in a second hole "ahole2".
Pratical example:
dotx is created with "ahole1" value = 10 and "ahole2" is empty.
dotx is imported into matlab
If the value of "ahole1"> 12, a second hole "ahole2" will have appended into it "5"
I am using matlab 2024b and the report generator to import the dotx, but i cannot find the means to get the content of a Hole unless from the script level i append a value to it.
  1 Kommentar
Image Analyst
Image Analyst am 9 Mär. 2025
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jack
Jack am 9 Mär. 2025
Hey Marcio,
If you're using MATLAB Report Generator with a .dotx template, you can access the existing values of holes (placeholders) using the Document Object Model (DOM) API. However, MATLAB’s built-in Report Generator functions don’t directly support reading placeholder values in .dotx templates before writing new content.
Workaround: Extract Pre-Filled Hole Values
You can use ActiveX (Windows only) or convert the document to .docx and read the content programmatically.
Using ActiveX to Read a Placeholder Value from a .dotx Template
% Open Word using ActiveX
word = actxserver('Word.Application');
word.Visible = false; % Set to true to see the document open
% Open the .dotx file
doc = word.Documents.Open('C:\path\to\template.dotx');
% Get all content controls (holes)
contentControls = doc.ContentControls;
% Loop through controls to find the value of "ahole1"
for i = 1:contentControls.Count
cc = contentControls.Item(i);
if strcmp(cc.Tag, 'ahole1') % Check if it's the hole we need
value_ahole1 = cc.Range.Text;
break;
end
end
% Close Word
doc.Close(false);
word.Quit;
delete(word);
disp(['ahole1 value: ', value_ahole1]);
How to Use This
  1. Replace 'C:\path\to\template.dotx' with your actual file path.
  2. Ensure Word is installed, as ActiveX requires Microsoft Word.
  3. Modify the script to check value_ahole1 before writing to ahole2.
Next Step: Modify "ahole2" Based on "ahole1"
After reading ahole1, you can append text to ahole2 using the MATLAB Report Generator API:
import mlreportgen.dom.*;
doc = Document('myreport.docx', 'docx');
% Create a hole and fill it based on ahole1 value
if str2double(value_ahole1) > 12
append(doc, 'ahole2', '5');
end
close(doc);
This should help you extract pre-filled values before updating other placeholders.
Follow me so you can message me anytime with future MATLAB questions. If this helps, please accept the answer as well.

Kategorien

Mehr zu MATLAB Report Generator finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by