How to get values of pre-filled holes in dotx template
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2025-3-9
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
回答(1 个)
Jack
2025-3-9
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
- Replace 'C:\path\to\template.dotx' with your actual file path.
- Ensure Word is installed, as ActiveX requires Microsoft Word.
- 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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Report Generator 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!