MATLAB Answers

App designer, export an image to word

9 views (last 30 days)
Hi all,
I am writing a small program in App designer that calculates a few things and then it inserts the results in a Word document.
Moreover in should also insert an a corresponding image for the calculation - say a free body diragram.
The first part of wirting the results to word is not a problem, but i am struggling with the last part of inserting an image.
The images is small drawings in PNG formate i made myself.
Heres is a small section from what i got so fare:
%Opning a wordfile, called Solutions
fid = fopen('Solutions.doc','w');
% æ ø å char(230),char(248),char(229)
%the first line in the word doc, writes "bulding height"
fprintf(fid,'building height');
%Adds line space
fprintf(fid,'\n');
%Writes h =, which is a abbreviation for height
fprintf(fid,'h = ');
%Inserts the user specified height of the buliding
fprintf(fid,'%d', h);
%Writes the unit of the userinput (height = meters)
fprintf(fid,' [m]\n');
%Adds a new linespacing
fprintf(fid,'\n');
%%%%%Insert the image building_height.PNG%%%%% here an image of my choosing of a building should be displayed in the word document.
I have looked through a lot of exsampels in here, also the Save2Word function. Non of it seems to work for me.
I hope my question makses sense, and thanks in advance
Regards Steffen

  0 Comments

Sign in to comment.

Accepted Answer

Nicolas B.
Nicolas B. on 4 Dec 2019
Edited: Nicolas B. on 4 Dec 2019
If you have the report generator, it would be the easiest way to fill a word document.
Otherwise, you should use the COM server to interact with word.
Edit: take a look at this file exchange.

  2 Comments

Steffen Cleveland
Steffen Cleveland on 4 Dec 2019
Hi Nicolas,
I tried this:
word = actxserver('Word.Application'); %start Word
word.Visible =1; %make Word Visible
document=word.Documents.Add; %create new Document
selection=word.Selection; %set Cursor
selection.Font.Name='Courier New'; %set Font
selection.Font.Size=9; %set Size
selection.Pagesetup.RightMargin=28.34646; %set right Margin to 1cm
selection.Pagesetup.LeftMargin=28.34646; %set left Margin to 1cm
selection.Pagesetup.TopMargin=28.34646; %set top Margin to 1cm
selection.Pagesetup.BottomMargin=28.34646; %set bottom Margin to 1cm
%1cm is circa 28.34646 points
selection.Paragraphs.LineUnitAfter=0.01; %sets the amount of spacing
%between paragraphs(in gridlines)
selection.InlineShapes.AddPicture([pwd '/picture.png'],0,1);
%with this command we insert a picture 'picture.png' wich is in the same
%folder as our m-file
selection.MoveDown(5,1);
selection.TypeParagraph;
selection.InsertNewPage;
document.SaveAs([pwd '/test.doc']); %save Document
word.Quit();
But i got this error:
"Server creation failed. Invalid ProgID 'Word.Application." any ideas?
And thanks for your reply
Nicolas B.
Nicolas B. on 4 Dec 2019
May I ask you to edit your post to put your code in code mode? It will be easier for everybody to read and follow your message.
Than, I tried your code with this configuration:
  • MATLAB R2019b
  • Office 365
What configuration do you have on your computer?
I haven't got problems with ActiveX or the COM-Server. However, I would recommand you to use fullfile to reconstruct path safely:
img = fullfile(pwd, 'picture.png');

Sign in to comment.

More Answers (1)

Steffen Cleveland
Steffen Cleveland on 4 Dec 2019
Hi Nicolas,
I have been working a bit with it now, and for some reason it works now.
Thank you!

  0 Comments

Sign in to comment.

Sign in to answer this question.