Saving a word document in MatLab 2021b

40 次查看(过去 30 天)
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 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by