Saving a word document in MatLab 2021b

조회 수: 55 (최근 30일)
George Pavlath
George Pavlath 2022년 10월 24일
댓글: George Pavlath 2022년 10월 31일
I have written a script that opens a figure file, which has multiple figures in it and returns a figure array. It then creates a artxserver for the Word application and creates a new word document. It then copies and pastes the figures sequentially to the Word document. The script works to this point and I have a Word document with the figures sequentially pasted into it. I would like the script to then save the Word document and then close the Word document. For closing, I use word.Quit(). The variable word is created earlier by word=actxserver('Word.Application'). The problem is to save the Word document before closing Word. I have looked in the MathWorkssupport section and have found this syntax: "document.SaveAs([path filename])". The variable document is created by document=word.Document.Add, which creates the blank Word document in the script. I get the error "unrecognized function or variable 'SaveAs' ". I cannot find the proper syntax to save the newly created Word document in MatLab. I am using Word 2016. Any suggestions are appreciated. Thank you.

답변 (1개)

Vishesh
Vishesh 2022년 10월 27일
  • The following code snippet explains how to specify a word template using MATLAB.
  • You can modify this code accordingly.
% Create a Word Automation Server and make it visible on the screen.
% This would be the equivalent of opening MS Word
word = actxserver('word.application');
word.visible =1;
%Pass the fully qualified name of document template that you want to use
doc1 = word.documents.Add('C:\Users\~\AppData\Roaming\Microsoft\QuickStyles\MyStyle1.dotx');
%Save the document. If the full qualified file name is not given then the
%file is saved to My Documents on Windows OSs
word.ActiveDocument.SaveAs2('Test.docx');
% Delete the handles that were created
word.Quit
  댓글 수: 1
George Pavlath
George Pavlath 2022년 10월 31일
This works. The key is the "word.ActiveDocument.SaveAs2('Test.docx')" command. Thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by