How do I manipulate table cells in microsoft word with matlab to change width and merge cells?
8 views (last 30 days)
Show older comments
I am using ActX to create a table in Word but I am having difficulties manipulating the table. There are two things that I would like to be able to do that I can't figure out.
1) How can I adjust the width of a cell? For example with the attached code the first row is 3 columns and the next 4 rows are 5 columns. I would like the first cell in row 1 to be the same width as the first column in rows 2-5 and I would like the second cell to be the same width as column 2 and 3 in the remaining rows.
2) How do I merge cells?
Thanks, Ian
function ER_Test
WordFileName='TestingDocument.doc';
CurDir=pwd;
FileSpec = fullfile(CurDir,WordFileName);
[ActXWord,WordHandle]=StartWord(FileSpec);
Style = 'Normal';
TextString = 'Valve Information';
Color = 'wdBlack';
WordText(ActXWord,TextString,Style,[0,0],Color,1,0)
DataCell = {' ','Head End','Crank End'};
WordCreateTable0(ActXWord,1,3,DataCell,0);
DataCell = {'','Suction','Discharge','Suction','Discharge'
'Valve Type:','1','2','3','4'
'Quantity:', '5','6','7','8'
'Lift:', '9','10','11','12'};
WordCreateTable0(ActXWord,4,5,DataCell,0);
function WordCreateTable0(actx_word_p,nr_rows_p,nr_cols_p,data_cell_p,enter_p) %Add a table which auto fits cell's size to contents if(enter_p(1)) actx_word_p.Selection.TypeParagraph; %enter end %create the table %Add = handle Add(handle, handle, int32, int32, Variant(Optional))
actx_word_p.ActiveDocument.Tables.Add(actx_word_p.Selection.Range,nr_rows_p,nr_cols_p,1,0);%bymargin
%write the data into the table
for r=1:nr_rows_p
for c=1:nr_cols_p
%write data into current cell
WordText(actx_word_p,data_cell_p{r,c},'Normal',[0,0],'wdBlack',0,1);
if(r*c==nr_rows_p*nr_cols_p)
%we are done, leave the table
actx_word_p.Selection.MoveDown;
else%move on to next cell
actx_word_p.Selection.MoveRight;
end
end
end
return
1 Comment
Bill Rooker
on 2 Dec 2016
Hello everyone,
I also need to know how to merge cells. I am writing multiple tables to a document so I need to have it where I can automate it.
Thanks,
Bill
Answers (0)
See Also
Categories
Find more on ActiveX 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!