Trying to access Word doc with MATLAB COM interface returns empty object
6 views (last 30 days)
Show older comments
MathWorks Support Team
on 13 May 2021
Answered: MathWorks Support Team
on 26 Jul 2021
I am trying to read the contents of a Word document from MATLAB R2019b using the COM interface. However, even the most simple operations are showing that there is no content in my document, which is not the case:
word = actxserver('Word.Application');
wdoc = word.Documents.Open('C:\somewhere\somefile.docx');
wdoc.Content.Text %indicates that there is no text in my document
wdoc.Sections %indicates that there are 0 sections in my document
MATLAB is not showing any errors or warnings during this process, yet the output is still incorrect. How can I fix this?
Accepted Answer
MathWorks Support Team
on 13 May 2021
1) Make sure that you can open the file in Word outside of MATLAB, just to verify that it isn't locked.
2) Open MATLAB with Administrative privileges. See the following MATLAB Answers post, which explains how to do this on Windows:
3) In the command window, register MATLAB as a COM server and verify the release version:
>> ! matlab -regserver
A new MATLAB command window will be created. Verify the version is the same as the current MATLAB version using
>> version
Then close the window.
4) Run the following MATLAB code, which should output the content text and section headings of your document:
filename = 'C:\somewhere\somefile.docx'; %replace this with the full path of your document
word = actxserver('Word.Application');
word.Visible = 1;
wdoc = word.Documents.Open(filename);
content = wdoc.Content.Text %make sure this output is correct
sections = wdoc.Sections %make sure this output is correct
Quit(word);
delete(word);
0 Comments
More Answers (0)
See Also
Categories
Find more on Use COM Objects in MATLAB 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!