How can I copy figures into Word using the COM interface in MATLAB?

23 views (last 30 days)
How can I copy figures into Word using the COM interface in MATLAB?
I am generating multiple figure windows in MATLAB and would like to programmatically copy these figures into Microsoft word.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 Jan 2017
The following lines of code illustrates how you would do this using the COM Automation support that is available in MATLAB:
 
% 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');
set(word,'visible',1);
% Create a new document
doc1 = invoke(word.documents,'add');
% Specify the location by creating a range
range = invoke(doc1,'range');
x = 0:0.1:2*pi;
y = sin(x);
numOfFigures = 10;
% Loop through and create the figures
for n = 1:numOfFigures
% Adds a new paragraph
invoke(doc1.Paragraphs,'Add');
% Select the new paragraph range
range = invoke(doc1,'range',doc1.Paragraphs.Item(n).Range.Start);
% Create a MATLAB Figure and copy it to the clipboard.
if mod(n,2) == 0
plot(1:10)
else
plot(x,y,'r-*');
end
print(gcf,'-dmeta');
% Paste the figure into the Word Document range that was previously selected
invoke(range,'Paste')
end
% Delete the handles that were created
delete(word)
More information about COM Automation support in MATLAB is found here:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!