MATLAB Answers

Having issues appending text to the header on a DOCX Report

3 views (last 30 days)
Gerardo Felix
Gerardo Felix on 4 Dec 2020
Answered: Anmol Dhiman on 9 Dec 2020
I have a DOTX template where I've inserted multiple holes in the body of the template that I've been able to successfully access and append to programmatically using the DOM API but I have not had the same luck with holes within the header of the template.
I've been attempting to access the headers using This results in a DOXPageHeader with the following properties:
DOCXPageHeader with properties:
PageType: 'default'
TemplateName: ''
TemplatePath: 'C:\engapps\Matlab\R2019b\toolbox\shared\mlreportgen\dom\resources\templates\docx\default.dotx'
Type: 'DOCX'
CurrentHoleId: '#end#'
CurrentHoleType: 'Block'
CurrentPageLayout: []
OpenStatus: 'open'
Parent: []
Children: [1×4 mlreportgen.dom.TemplateText]
Tag: 'dom.DOCXPageHeader:333564901'
Id: '333564901'
In the template, I've inserted 2 holes in the header but on the output above, it appears to ignore them and go straight to #end#.
The body of my template is broken up into the following holes:
  • #start#
  • #sect2#
  • Body-Hole1
  • Body-Hole2
  • #sect3#
  • #end#
I've noticed that under hole #sect3#, its PageHeader actually starts at hole #start#, but when I move to the next hole, it goes to #end#.
I have also attempted the few examples I've found and also have not been successful appending the a header programmatically...
In summary, the issues I'm having is accessing the holes I've inserted into my DOTX template file.

Answers (1)

Anmol Dhiman
Anmol Dhiman on 9 Dec 2020
Hi Felix,
Holes in a header can be accessed from the current DOCX section. You can use the following to access the first section's header information:
d = mlreportgen.dom.Document('mydoc','docx','WordTemplate.dotx');
docHeaders = d.CurrentDOCXSection.PageHeaders;
The "docHeaders" will be an array of headers with "PageType" properties of "first", "default", or "even". Descriptions of what these types mean are at the following link:
You can select the header for the page type you would like and then use the "moveToNextHole" method to progress through the holes in the header:
docHead1 = docHeaders(1);
holeId = docHead1.moveToNextHole;
You can also move through the holes in the main body of the section with the following:
nextHoleID = d.moveToNextHole;
If the document has multiple sections, then as you progress through the holes in the document, you will encounter holes with ID's like "#sect<x>#", where "<x>" is the section number. Once the "CurrentHoleId" of the document has this ID, then you can access the headers of the new section in the same way as above using "d.CurrentDOCXSection".
Footers can also be accessed with a similar method
docFooters = d.CurrentDOCXSection.PageFooters;
Hope it Helps
Anmol Dhiman




Community Treasure Hunt

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

Start Hunting!