Trying to access Word doc with MATLAB COM interface returns empty object
조회 수: 5 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2021년 5월 13일
답변: MathWorks Support Team
2021년 7월 26일
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?
채택된 답변
MathWorks Support Team
2021년 5월 13일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!