MATLAB: How to access holes in the header of a document with multiple sections using MATLAB Report Generator DOM API

docxdotxfooterfootersgeneratorheaderheadersMATLAB Report Generatormlreportgenreportsectionsectionsword

I have a Word document template with multiple sections, and each section has its own header with various holes. How can I access the holes in the headers of different sections?

Best Answer

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');
>> open(d);
>> 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;