Word COM - writing text into this open document
조회 수: 26 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Eric
2011년 2월 17일
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
댓글 수: 1
Walter Roberson
2018년 9월 4일
zhichuan wang comments,
With the following command, I can insert external picture to word.
actx_word_p.application.selection.InlineShapes.AddPicture('D:\Matlab_files\WriteToWordWithMatlab\Fail.png');
추가 답변 (3개)
Kaustubha Govind
2011년 2월 17일
Once you start Word as a COM server from MATLAB, it is basically just a matter of invoking methods from the MS Word COM API. The Reference is here: http://msdn.microsoft.com/en-us/library/ff841702.aspx
댓글 수: 1
Robert
2011년 11월 11일
Hello,
I'm also trying to open a certain word-file and to add some graphics. At the moment it is possible to open a certain existing word-document. I would like to ask you to give me an example how to add a new page in this document to add a new graphic. How can I also select a certain page of the word document?
kind regards,
Robert
댓글 수: 1
Eric
2011년 11월 23일
To go to a particular page you should be able to use something like:
wdGoToPage = 1;
wdGoToNext = 2;
Word_COM.Selection.GoTo(wdGoToPage, wdGoToNext, 1, desired_page);
where desired_page is the page number you want.
To insert a page break you can use
wdPageBreak = 7;
Word_COM.Selection.InsertBreak(wdPageBreak);
Good luck,
Eric
참고 항목
카테고리
Help Center 및 File Exchange에서 Use COM Objects in MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!