MATLAB: Word COM – writing text into this open document

niveko

I need to know how to write text into an open Word document. I used this code to open the word application:
word = actxserver('word.application');
set(word,'visible',1);
doc1 = invoke(word.documents,'add');
invoke(doc1.paragraphs,'add');
range = invoke(doc1,'range',doc1.Paragraphs.Item(1).range.Start);
I need to write several lines of text and two plots into this document. How do I do this?

Best Answer

Let Word_COM be the COM object associated with MS Word. You can use
Word_COM.Selection.TypeText('string');
Word_COM.Selection.TypeParagraph;
To add some text and a carriage return. Use
Pic = Word_COM.Selection.InlineShapes.AddPicture('picture_filename');
set(Pic, 'ScaleHeight', scale, 'ScaleHeight', scale);
Word_COM.Selection.MoveRight(1);
Word_COM.Selection.TypeParagraph;
To insert a picture (which can be a PNG or some file created from a Matlab figure), adjust the size, and then enter a carriage return.
Good luck,
Eric