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

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

Atleast show us some code/effort to open ActiveX server connection and open word your file. Please don't expect fully working code from scratch.
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

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

10 Comments

That's what i've been looking for. Thank you!
Milutin
The presented script tabulates the <filt_pres> data which I cannot find. Could someone provide it, or instruct where to search for.
Thank you in advance.
No idea, what milutin means.
The filt_pres table is proprietary data, which is not available anywhere and which I will certainly not make available. The above was provided so that the OP could understand how to insert and format tables in Word documents, the actual content of filt_pres is irrelevant for that, you can make it up if you wish.
Thanks for the reply.
Sorry, I had no idea that filt_pres table is proprietary data, sorry. I thought that it is within Matlab package and I am short of skills to find it.
Milutin = my nick name, used with intention to appear in the comment as identification, instead of ZM. I am commenting/asking for the assistance for the first time, so did not know the style.
Thank you for your code. It's very helpful.
One question, how can I merge two columns in the same row instead of merging the whole columns?
You'd do it this way:
%wtable: a word table object
%rowidx: scalar, the row on which the two cells to merge are. 1-based
%colidx: two element array, the two columns to merge on the row. 1-based
trow = wtable.Rows.Item(rowidx);
trow.Cells.Item(colidx(1)).Merge(trow.Cells.Item(colidx(2)))
please is it possible to insert a table from matlab to word with actxserver?
>>please is it possible to insert a table from matlab to word with actxserver?
Well, yes, that's what my example does
How to change background color of table made in PPT through actxserver.
h = actxserver('PowerPoint.Application')
h.Visible = 1;
%% ADD PRESENTATION
h.Presentation.invoke
Presentation = h.Presentation.Add
Presentation.Slides.invoke
blankSlide = Presentation.SlideMaster.CustomLayouts.Item(1);
Slide1 = Presentation.Slides.AddSlide(1,blankSlide);
%% ADD TABLE
table = invoke(Slide1.Shapes, 'AddTable',8, 2);
table.Table.Cell(1,1).Shape.TextFrame.TextRange.Text = 'Name';

Sign in to comment.

More Answers (0)

Products

Asked:

on 24 May 2018

Commented:

on 5 Jan 2023

Community Treasure Hunt

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

Start Hunting!