Saving a Word Document using actxserver

57 views (last 30 days)
Stephanie
Stephanie on 28 Jun 2013
Commented: Varun Bansal on 29 Nov 2021
Hi,
I have a GUI that I made in guide that creates graphs based on user inputs. I have button labeled "create report". When that button is pressed all the important data from the graphs are saved into a microsoft word document using fprint. This all works fine, but I want to add the picture of the graphs as well, using the following code:
%copy to clipboard
print -dmeta
% open MS-Word
Word = actxserver('Word.Application');
Word.Visible = true;
%open the document
doc = invoke(Word.Documents,'Open',FileSpec);
% paste figure from clipboard
Word.Selection.Paste
%crop the image
doc.InlineShapes.Item(1).PictureFormat.CropBottom = 280;
doc.InlineShapes.Item(1).PictureFormat.CropRight = 560;
doc.InlineShapes.Item(1).PictureFormat.CropTop = 85;
doc.InlineShapes.Item(1).Height=270;
doc.InlineShapes.Item(1).Width=400;
%save the document
File = [FileLocation FileName];
invoke (doc, 'save')
%invoke (doc, 'SaveAs', File)
%doc.save
%invoke(Word,'Quit');
% delete(Word);
When the word doc is opened and made visible MS word makes me choose how I would like to view the document. The word winds says that the file is in use by another user and then gives me the following options: "Open a read only copy", "create a local copy and save your changes later", or "receive notification when the original copy is available". I generally chose "open a read only copy", but I have tried the others and they aren't any better. After I make that viewing selection the images gets pasted into the document and cropped like its supposed to, but the image won't save. I have tried the 3 methods I have listed in the code and none of them work. I generally get this error:
Error using
Interface.0002096B_0000_0000_C000_000000000046/invoke
Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: The Save method or property is not available
because the current document is not available in full screen
reading.
Help File: C:\Program Files (x86)\Microsoft
Office\Office12\1033\WDMAIN11.CHM
Help Context ID: 91fd
Error in AnalysisMultipleSubjectGUI>ReportPushButton_Callback
(line 6295)
invoke (doc, 'save')
I don't actually need to view the document as the report is made, but if I don't have the document visible during this, then the image won't even paste in until the file is opened. If I close the file then the image doesn't save. I can manually save it, but I have a lot of reports to make so I would rather avoid that if possible. Also, if I manually save it, MS word won't let me save it as a 97-2003 document (which is what the matlab-made document is) because matlab is still using it. So the next best option is to save it as another file type, in which case I have 2 report files.
Any ideas as to how to save the image in the same document as the rest of the report (preferably without having to open MS word)?
Also, a random question. How do I insert the image into a specific line of the document (not at the beginning or end of the document)?
THANKS!!

Answers (0)

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!