MATLAB Answers

0

reading tables from .doc word file

Asked by vignashwaran s on 28 Aug 2019
Latest activity Commented on by vignashwaran s on 30 Aug 2019
hi ... i can able to read a .doc file but it have some tables when i try to, read all txt in table comes in a single cell ..any one having code for reading table from word file .doc

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Cris LaPierre
on 28 Aug 2019
 Accepted Answer

Here's some starter code for you. Be aware that this code assumes that, if there are multiple tables, they are all the same size.
%% open the doc
word = actxserver('Word.Application');
% Open/Select Word file
[filename_in, pathname] = uigetfile({'*.doc;*.docx;*.docm','Word Files (*.doc,*.docx,*.docm)'; ...
'*.*', 'All Files (*.*)'}, 'Select a file');
document = word.documents.Open(fullfile(pathname,filename_in));
r=0;
tbl_cnt = document.Tables.Count;
% Loop through each table in the document
for tbl = 1 : tbl_cnt
row_cnt = document.Tables.Item(tbl).Rows.Count;
col_cnt = document.Tables.Item(tbl).Columns.Count;
for row = 1 : row_cnt
r = r+1;
for col = 1 : col_cnt
% Pull the values from the table
cell_txt = strtrim(document.Tables.Item(tbl).Cell(row, col).Range.Text);
% Add each value to its own cell
tblVals{r,col} = cell_txt;
end
end
end
% Close the document.
document.Close;
% Close Word.
word.Quit;
% delete server object
delete(word);
clear word document

  1 Comment

Thanks Cris.. thanks for repying, it's working.

Sign in to comment.