MATLAB: How to paste figures in an opened ms word document? (programmatically using COM Automation server))

activexms wordword

Is it possible to connect to an opened or active MSword document (the one I'm editing at that moment) and paste any matlab figures? There are a lot of answers were MSword documents are opened or created, but now I want to paste some matlab results into an opened document. I think it must be something like this:
  1. let script connect to the active document using activex server
  2. copy figure programmatically
  3. paste matlab results in word doc programmatically
  4. release connection and leave doc open (so that I can proceed documentation in MSword)
Regards, Patrick

Best Answer

Thanks for all the answers. The following code is what I was looking for. Before running this script you have to open a MS word document. Position the cursor at the position where you want to paste the figure.
% Generate test plot
x = linspace(0,2*pi,1000);
plot(x,sin(x));
% Copy current figure
print(gcf, '-dmeta')
% Get a handle to the running Microsoft Word program:
h = actxGetRunningServer('word.Application');
% Command line notification
disp(['Pasting a figure in ' h.ActiveDocument.Name ])
% Pastespecial as Shape
h.selection.PasteSpecial(0,0,1,0,3)
% Modify shape size (72 points in one inch) and convert to
% InlineShape. Move cursor one position to the right for the next action.
h.selection.ShapeRange.Width = 1*72; % width = 1inch
h.selection.Shaperange.ConvertToInlineShape
h.selection.Start = h.selection.Start+1;
h.selection.End = h.selection.End+1;
% End notification (optional)
h.selection.TypeText('end')
Patrick
Related Question