reading tables from .doc word file

22 views (last 30 days)
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

Accepted Answer

Cris LaPierre
Cris LaPierre on 28 Aug 2019
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
  2 Comments
vignashwaran s
vignashwaran s on 30 Aug 2019
Thanks Cris.. thanks for repying, it's working.
João
João on 7 Nov 2022
wdmain11.chm dependency , be aware.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!