Create and fill in a table in Microsoft Word with Actx Server from MATLAB
46 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Guillaume
le 24 Mai 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
10 commentaires
Guillaume
le 12 Avr 2021
>>please is it possible to insert a table from matlab to word with actxserver?
Well, yes, that's what my example does
Shivam Mishra
le 5 Jan 2023
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';
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Use COM Objects in MATLAB dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!