Saving a word document in MatLab 2021b

56 views (last 30 days)
George Pavlath
George Pavlath on 24 Oct 2022
Commented: George Pavlath on 31 Oct 2022
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.

Answers (1)

Vishesh
Vishesh on 27 Oct 2022
  • 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 Comment
George Pavlath
George Pavlath on 31 Oct 2022
This works. The key is the "word.ActiveDocument.SaveAs2('Test.docx')" command. Thank you.

Sign in to comment.

Categories

Find more on Printing and Saving 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!