Word COM - writing text into this open document

26 visualizaciones (últimos 30 días)
Kevin O'Hara
Kevin O'Hara el 17 de Feb. de 2011
Comentada: Walter Roberson el 4 de Sept. de 2018
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?

Respuesta aceptada

Eric
Eric el 17 de Feb. de 2011
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 comentario
Walter Roberson
Walter Roberson el 4 de Sept. de 2018
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');

Iniciar sesión para comentar.

Más respuestas (3)

Kaustubha Govind
Kaustubha Govind el 17 de Feb. de 2011
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 comentario
Feng Guo
Feng Guo el 31 de Jul. de 2018
These methods were what I was trying to find, thanks a lot!

Iniciar sesión para comentar.


Kevin O'Hara
Kevin O'Hara el 17 de Feb. de 2011
Is there example MatLab code that shows me how to add text, such as:
Flight Test Summary - T1-ETN41
Flight Test: T1_ETN41_v3 Flight Date: Oct 21, 2010
Task 68: \Test_Data\T1\000410000\T1_F00041_G0000_T68_Merged_V3.h5 • T1_ETN41_T68_e2.mat • T1_ETN41_T68_e3.mat • T1_ETN41_T68_e4.mat • T1_ETN41_T68_e5.mat • T1_ETN41_T68_e6.mat • T1_ETN41_T68_e7.mat • T1_ETN41_T68_e8.mat • T1_ETN41_T68_e9.mat • T1_ETN41_T68_e10.mat • T1_ETN41_T68_e11.mat • T1_ETN41_T68_e12.mat • T1_ETN41_T68_e13.mat • T1_ETN41_T68_e14.mat
and then a new page

Robert
Robert el 11 de Nov. de 2011
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 comentario
Eric
Eric el 23 de Nov. de 2011
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

Iniciar sesión para comentar.

Categorías

Más información sobre Use COM Objects in MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by