No BSD License  

Highlights from
save2word

4.58824

4.6 | 17 ratings Rate this file 99 Downloads (last 30 days) File Size: 2.99 KB File ID: #3149

save2word

by Suresh Joel

 

17 Mar 2003 (Updated 28 Mar 2003)

Paste figure to Microsoft word automatically using ActiveX.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

A simple function to copy figure from MATLAB into MS Word automatically. It is a modification of saveppt (a function in File exchange) that save figures to MS Powerpoint.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
saveppt
This submission has inspired the following:
Quick_Figure, OfficeDoc - read/write/format MS Office docs (XLS/DOC/PPT), WriteToWordFromMatlab

MATLAB release MATLAB 6.1 (R12.1)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (21)
21 Mar 2003 vishnu natesan  
03 Apr 2003 Matthias Wetzel

Fine, wonderful tool.
Furthermore I'm searching for "own" report generator. This is one step to get it.

19 Feb 2004 Kasper Paasch

Wonderfull tool. Just what I needed. Thanks.

05 May 2004 Gang Huang

Splendid

10 Nov 2004 Fabien Lahoche

Very nice tool. Thks

24 Nov 2004 I. Kalatzis

!

25 Nov 2004 Jonathan Archer

Change the words 'PowerPoint' in the file to 'Word', perhaps.

18 May 2006 Lee Barnea

nice useful tool. a bit problematic when the destination file is already opened

22 Jun 2007 Sylvia Kohn-Rich

The error:
?? Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: This method or property is not available because the
Clipboard is empty or not valid.
Help File: C:\Program Files\Microsoft
Office\OFFICE11\1033\wdmain11.chm
Help Context ID: 91fd

can be avoided by using the following version:

function save2word(filespec,prnopt)

% SAVE2WORD saves plots to Microsoft Word.
% function SAVE2WORD(filespec,prnopt) saves the current Matlab figure
% window or Simulink model window to a Word file designated by
% filespec. If filespec is omitted, the user is prompted to enter
% one via UIPUTFILE. If the path is omitted from filespec, the
% Word file is created in the current Matlab working directory.
%
% Optional input argument prnopt is used to specify additional save
% options:
% -fHandle Handle of figure window to save
% -sName Name of Simulink model window to save
%
% Examples:
% >> saveppt
% Prompts user for valid filename and saves current figure
% >> save2word('junk.doc')
% Saves current figure to MS Word file called junk.doc
% >> save2word('junk.doc','-f3')
% Saves figure #3 to MS Word file called junk.doc
% >> save2word('models.doc','-sMainBlock')
% Saves Simulink model named "MainBlock" to file called models.doc
%
% The command-line method of invoking SAVEPPT will also work:
% >> save2word models.doc -sMainBlock
%
% If the figure has to be pasted as a smaller size bitmap, go to
% File->preferences->Figure Copy Template->Copy Options and
% check "Match Figure Screen Size" checkbox.
% Then make the figure small before by setting the position
% of the figure to a smaller size using
% set(gca,'Position',[xpos,ypos,width,height])
%
% Check also saveppt in Mathworks fileexchange

%Suresh E Joel, Mar 6,2003
%Virginia Commonwealth University
%Modification of 'saveppt' in Mathworks File Exchange
%and valuable suggestions by Mark W. Brown, mwbrown@ieee.org
%Sylvia Kohn-Rich:MODIFIED TO AVOID WORD ERROR

% Establish valid file name:
if nargin<1 | isempty(filespec);
  [fname, fpath] = uiputfile('*.doc');
  if fpath == 0; return; end
  filespec = fullfile(fpath,fname);
else
  [fpath,fname,fext] = fileparts(filespec);
  if isempty(fpath); fpath = pwd; end
  if isempty(fext); fext = '.doc'; end
  filespec = fullfile(fpath,[fname,fext]);
end

% Start an ActiveX session with PowerPoint:
word = actxserver('Word.Application');
%word.Visible = 1;

if ~exist(filespec,'file');
   % Create new presentation:
  op = invoke(word.Documents,'Add');
else
   % Open existing presentation:
  op = invoke(word.Documents,'Open',filespec);
end

% Find end of document and make it the insertion point:
end_of_doc = get(word.activedocument.content,'end');
set(word.application.selection,'Start',end_of_doc);
set(word.application.selection,'End',end_of_doc);

% Capture current figure/model into clipboard:
% THIS BLOCK WAS MOVED
if nargin<2
  print -dmeta
else
  print('-dmeta',prnopt)
end

% Paste the contents of the Clipboard:
invoke(word.Selection,'Paste');

% ADD PAGE BREAK
invoke(word.Selection,'InsertBreak',7);

if ~exist(filespec,'file')
  % Save file as new:
  invoke(op,'SaveAs',filespec,1);
else
  % Save existing file:
  invoke(op,'Save');
end

% Close the presentation window:
invoke(op,'Close');

% Quit MS Word
invoke(word,'Quit');

% Close PowerPoint and terminate ActiveX:
delete(word);

return

05 Nov 2007 Ma Liang

I also have some error as below:
??? Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: ????????????
Help File: C:\Program Files\Microsoft Office\OFFICE11\2052\wdmain11.chm
Help Context ID: 62e2

Error in ==> WriteToWordFromMatlab>WordText at 198
actx_word_p.Selection.Style = style_p;

Error in ==> WriteToWordFromMatlab at 29
    WordText(ActXWord,TextString,Style,[0,2]);%two enters after text

06 Dec 2007 Licente Ro

I noticed in case one wants to automize this process and save multiple figures one after the other, there is a need to give some time to WORD to properly close both the file and Template.dot.

I intorduced some delay in the loop.
Initially I used 1 sec, but this is too low for my computer. With 2 sec works better.

Thanks again for this breaktrough info.
Licente

03 Apr 2008 Erik Larsen

I only tried this file b/c I ran into a matlab bug where I tried to copy a figure from the Edit->Copy Figure menu into word, but matlab was giving me an error (something about the figure handle not being valid, which was clearly a matlab bug). I spent a long time trying to find a workaround, but nothing worked well.

Suresh' script worked beautifully though, it generated a word file with the figure in there, exactly as it appeared in matlab (had to check 'match figure screen size' in the Edit->Copy Figure Options menu, though).

17 Apr 2008 Dan Mikulskis

this is really handy. does exactly what i wanted.

18 Apr 2008 Lefteris Kaskavelis

I tried to use this code in MATLAB2007b in WindowsXP and I get the following message.
------------------------------------------
Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: This method or property is not available because the Clipboard is empty or not valid.
Help File: C:\Program Files\Microsoft Office\OFFICE11\1033\wdmain11.chm
Help Context ID: 91fd

Error in ==> save2word at 80
invoke(word.Selection,'Paste');
---------------------------------------

It used to work without problems in MATLAB2006b and Windows2000

Any ideas are welcome.

Thanks in advance

23 Apr 2008 Barbara Robson

If you replace "print -dmeta" with "uimenufcn(gcf,'EditCopyFigure')" you get exactly the image you'd get if you used Edit->Copy->Figure and pasted it manually: I find that this gives superior results for my purposes. For example, using uimenufcn gives me more control over the figure size and axis background color.

16 Sep 2008 Anup Khekare

It works perfectly fine for me. I was looking for a way to code this, but this code here solves all the problems for me. Thank you very much. This helps to cut short the save figure and paste figure process.

Thanks Again.

26 Nov 2008 Cristian Gutierrez Acuna

>> save2word('junk.doc')
??? Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: This method or property is not available because the Clipboard is empty or not valid.
Help File: C:\Program Files\Microsoft Office\Office12\1033\WDMAIN11.CHM
Help Context ID: 91fd

Error in ==> save2word at 80
invoke(word.Selection,'Paste');

25 Jun 2009 Alex Frid

very handy! thanks!

18 May 2010 Wenxu Li

very good tool,thank you

21 Jun 2010 esya esya

It helped me finding way to read text data in the doc file.

Thanks a lot.

03 Nov 2011 Lokesh

Thanks Suresh... Its a very helpful program. It has saved a lot of time for me.

Please login to add a comment or rating.
Updates
28 Mar 2003

Mark Brown put in some invaluable tips to add the figure to the end of the file instead of the beginning!

Tag Activity for this File
Tag Applied By Date/Time
development environment Suresh Joel 22 Oct 2008 06:58:30
word Suresh Joel 22 Oct 2008 06:58:30
activex Suresh Joel 22 Oct 2008 06:58:30
microsoft Suresh Joel 22 Oct 2008 06:58:30
utilities Suresh Joel 22 Oct 2008 06:58:30
development Suresh Joel 22 Oct 2008 06:58:30
potw Cristina McIntire 07 Nov 2008 13:23:11
activex jaco jaco 15 Jun 2010 05:53:58
word Hans Hoelzer 15 Jul 2010 13:29:49
activex Fabrice 19 Jan 2011 09:55:22
pick of the week Jiro Doke 11 Feb 2011 20:18:02

Contact us at files@mathworks.com