Create and fill in a table in Microsoft Word with Actx Server from MATLAB

97 views (last 30 days)
ABCD
ABCD on 24 May 2018
Commented: Guillaume on 12 Apr 2021
Hello,
I want to create a table and fill it with variables in Microsoft Word with the Actx Server from MATLAB. I searched a lot but couldn't find a solution.
I also tried some available functions (toTable.m, table2word.m,..) but they didn't work at all.
It would be great if somebody could show me how to create the following table:
The table should have 7 rows and 2 coloums. Another thing i'd like to know is how to merge or divide cells in this table.
I am using Word 2013 and MATLAB R2011b.
Thank you in advance!
  2 Comments
ABCD
ABCD on 24 May 2018
I'm able to open word and fill it with variables. The only thing i'd like to get is the code for the table, because i can't find a way to "translate" VBA to MATLAB Actx syntax.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 24 May 2018
Here is part of some code I wrote several years ago that adds a long table to an existing word document. The word table was filled from the content of a matlab table (filt_pres). There's a lot of stuff there that is of no interest to you but it shows how to:
  • add a table
  • merge cells
  • fill the table with text
  • change the style of the text (using styles already defined in the document)
word = actxserver('Word.Application');
word.Visible = true;
wdoc = word.Documents.Open(docname);
Selection = word.Selection;
Selection.InsertBreak(5); %5 is wdSectionBreakOddPage
Selection.MoveLeft(1, 1); %1 is wdCharacter
%Create table and format properly. The format of the table is:
% Contents
% Session 1
% paper 1 | page ref
% ... | ...
% paper n | page ref
% Session 2
% paper 1 | page ref
% ...
%Hence the table has 2 columns and 1 (contents) + number of papers + number of sessions rows
table = Selection.Tables.Add(Selection.Range, 1 + height(filt_pres) + numel(unique(strcat(filt_pres.Topic, num2str(filt_pres.TopicNumber)))), 2);
table.Columns.Item(1).SetWidth(400, 3);
table.Rows.AllowBreakAcrossPages = false;
%First row of table is the header. Just one cell with 'Contents' with the TOC Title style
table.Rows.Item(1).Cells.Merge;
table.Rows.Item(1).Range.Select;
Selection.Style = wdoc.Styles.Item('TOC Title');
Selection.TypeText('Contents');
%Now fill the session and paper rows
lasttopic = '';
lastnumber = 0;
%if the topic name or number of the current paper is different from lasttopic, it's the start of a new session
tablerow = 1; %to keep track of which is being fillef
for row = 1:height(filt_pres)
tablerow = tablerow + 1;
newtopic = filt_pres.Topic{row};
newnumber = filt_pres.TopicNumber(row);
if ~(strcmp(newtopic, lasttopic) && newnumber == lastnumber)
%start of a new session. Change row to a session row by merging the two columns and setting the style to TOC Topic
lasttopic = newtopic;
lastnumber = newnumber;
table.Rows.Item(tablerow).Cells.Merge;
table.Rows.Item(tablerow).Range.Select;
Selection.Style = wdoc.Styles.Item('TOC Topic');
Selection.TypeText(sprintf('%s %d', lasttopic, lastnumber));
tablerow = tablerow + 1;
end
%fill row with paper and reference. Paper cell is made of two lines with two different styles
%first line is paper title. second line is paper authors.
table.Cell(tablerow, 1).Range.Select
Selection.Style = wdoc.Styles.Item('TOC Paper Title');
Selection.TypeText(filt_pres{row, 5}{1}); %row 5 is title
Selection.TypeText(char(10));
Selection.Style = wdoc.Styles.Item('TOC Paper Authors');
authors = filt_pres{row, 6}{1};
authors = regexprep(authors, '\n.*', ''); %remove affiliations
authors = regexprep(authors, '\s*\([^)]*\)\s*', ''); %remove affiliations references
Selection.TypeText(authors);
Selection.Font.Italic = false;
table.Cell(tablerow, 2).Range.Select
Selection.Style = wdoc.Styles.Item('TOC Paper Page');
Selection.TypeText('');
Selection.InsertCrossReference(2, 7, strrep(filt_pres.Reference{row}, '-', ''), true); %2 is wdRefTypeBookmark, 7 is wdPageNumber, true for hyperlink
end
  9 Comments
Guillaume
Guillaume on 12 Apr 2021
>>please is it possible to insert a table from matlab to word with actxserver?
Well, yes, that's what my example does

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!