MATLAB report generator - Microsoft word header/footer 'Link to Previous' Section
8 views (last 30 days)
Show older comments
Hello,
I want to create a Microsoft word report . The first few pages are written directly from another program. The next pages are written from MATLAB based on word template already saved. I need the page headers and footers continued from the previous section. I was unable to find something which is similar to 'Link to Previous' as in word. Can someone help?
0 Comments
Answers (1)
Gayathri
on 7 Nov 2024
For appending data to an existing word, we can use the MATLAB's ActiveX interface. This will automatically get continued with the existing headers and footers.
Start by creating a Word ActiveX server and opening your template document. Please find the below code for using the Word ActiveX interface.
% Start an ActiveX session with Word
wordApp = actxserver('Word.Application');
wordApp.Visible = true; % Make Word visible for debugging
% Open the existing Word document
docPath = 'path\new1.docx';
doc = wordApp.Documents.Open(docPath);
% Move to the end of the document
selection = wordApp.Selection;
wdStory = 6; % The numeric value for wdStory
selection.EndKey(wdStory);
% Insert a section break (next page)
wdSectionBreakNextPage = 2; % The numeric value for wdSectionBreakNextPage
selection.InsertBreak(wdSectionBreakNextPage);
% Add new content after the section break
selection.TypeText('This is a new paragraph added after a section break.');
selection.TypeParagraph; % Add a new paragraph break
% Add a table (example with 2 rows and 2 columns)
selection.Tables.Add(selection.Range, 2, 2);
table = selection.Tables.Item(1);
table.Cell(1, 1).Range.Text = 'Header 1';
table.Cell(1, 2).Range.Text = 'Header 2';
table.Cell(2, 1).Range.Text = 'Row 1 Col 1';
table.Cell(2, 2).Range.Text = 'Row 1 Col 2';
% Save and close the document
doc.Save();
doc.Close(false);
wordApp.Quit;
For more information on “actxserver”, please refer to the following link.
Hope you find this information helpful.
3 Comments
See Also
Categories
Find more on MATLAB Report Generator in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!