MATLAB Examples

Word commands with actxserver

Software used: MatlabR2015b | Microsoft Office 2007
Author: Stefan Braun
March 2016; Last revision: 31.03.2016
This file shows some commands to control Word with Matlab.
Run step by step and see what it does

Contents

Initialization

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)

Write Text and Linebreak

selection.TypeText('Hello World!');         %write Text
selection.TypeParagraph;                    %line break

Mark Text -> change Font -> change Background color

selection.MoveUp(5,1,1);                    %5=row mode
%with this command we mark the previous row %1=amount
                                            %1=hold shift
selection.Font.Bold=1;                      %set text to Bold
selection.Shading.BackgroundPatternColorindex=7;
%with this command we dye the Background yellow
%other colors:   0->remove Highlighting
%                1->Black
%                2->Blue
%                3->Turquoise
%                4->Bright green
%                5->Pink
%                6->Red
%                7->Yellow
%                8->White
%                9->Dark blue
%               10->Teal
%               11->Green
%               12->Violet
%               13->Dark red
%               14->Dark yellow
%               15->Shade 50 of gray
%               16->Shade 25 of gray

selection.MoveDown(5,1);                    %5=row mode
%with this command we move the curser       %1=amount
%back down

Mark Text -> change Font -> Highlight Text -> change Font color

selection.TypeText('Not this one, ');       %write some text
text='Highlight this text';
selection.TypeText(text);                   %write String
selection.TypeParagraph;                    %linebreak
selection.MoveLeft(1,length(text)+1,1);     %1=character mode
%with this command we mark the previous text%length(text)+1=amount
                                            %1=hold shift
selection.Font.Italic=1;                    %set text to italic
selection.Range.HighlightColorIndex=9;
%with this command we Highlight text with a Textmarker
%other colors:   0->remove Highlighting
%                1->Black
%                2->Blue
%                3->Turquoise
%                4->Bright green
%                5->Pink
%                6->Red
%                7->Yellow
%                8->White
%                9->Dark blue
%               10->Teal
%               11->Green
%               12->Violet
%               13->Dark red
%               14->Dark yellow
%               15->Shade 50 of gray
%               16->Shade 25 of gray
selection.Font.Color=16777215;
%with this command we change the marked Font color
%Matlab only knows decimals so we have to change RGB Hex code to decimal
%Problem: Normal RGB code hast 24 Bits with blue from 0 to 7
%                                           green from 8 to 15
%                                           red from 16 to 23
%BUT we must send it in BGR!!!
%with this information we must change the order of the hexcode
%
%like:Red->Hex:#ff0000 invert->0000ff in decimal 255
%    Blue->Hex:#0000ff invert->ff0000 in decimal 16711680
%   Green->Hex:#00ff00 invert->00ff00 in decimal 65280
%   Black->Hex:#000000 invert->000000 in decimal 0
%   White->Hex:#ffffff invert->ffffff in decimal 16777215


selection.MoveDown(5,1);                    %5=row mode
%with this command we move the curser       %1=amount
%back down

Write Numbers

mark numbers -> change Underline color -> Underline with dashes

x=12345;

selection.TypeText(num2str(x));             %write number
selection.TypeParagraph;                    %linebreak
selection.MoveUp(5,1,1);                    %5=row mode
%with this command we mark the previous row %1=amount
                                            %1=hold shift
selection.Font.UnderlineColor=255;
%with this command we change the Underline color
%Matlab only knows decimals so we have to change RGB Hex code to decimal
%Problem: Normal RGB code hast 24 Bits with blue from 0 to 7
%                                           green from 8 to 15
%                                           red from 16 to 23
%BUT we must send it in BGR!!!
%with this information we must change the order of the hexcode
%
%like:Red->Hex:#ff0000 invert->0000ff in decimal 255
%    Blue->Hex:#0000ff invert->ff0000 in decimal 16711680
%   Green->Hex:#00ff00 invert->00ff00 in decimal 65280
%   Black->Hex:#000000 invert->000000 in decimal 0
%   White->Hex:#ffffff invert->ffffff in decimal 16777215

selection.Font.Underline=7;                 %Dashes
%with this command we change the Underline
%other options: 0->No Underline
%               1->A single line. default
%               2->Underline individual words only
%               3->A double line
%               4->Dots
%               6->A single thick line
%               7->Dashes
%               9->Alternating dots and dashes
%              10->An alternating dot-dot-dash pattern
%              11->A single wavy line
%              20->Heavy dots
%              23->Heavy dashes
%              25->Alternating heavy dots and heavy dashes
%              26->An alternating heavy dot-dot-dash pattern
%              27->A heavy wavy line
%              39->Long dashes
%              43->A double wavy line
%              55->Long heavy dashes

selection.MoveDown(5,1);                    %5=row mode
%with this command we move the curser       %1=amount
%back down

selection.InsertNewPage;                    %create new Page(cursor moves to new page)

Write Block of Text

selection.ParagraphFormat.Alignment =3;     %Fully jusified
%with this command we change the Format
%other formats: 0->Left-aligned
%               1->Center-aligned
%               2->Right-aligned
%               3->Fully justified
%               4->Paragraph characters are distributed to fill the entire
%                  width of the paragraph
%               5->Justified with a medium character compression ratio
%               7->Justified with a high character compression ratio
%               8->Justified with a low character compression ratio
%               9->Justified according to Thai formatting Layout

selection.TypeText('There was an old sow with three little pigs, and as she had not enough');
selection.TypeText(' to keep them, she sent them out to seek their fortune. The first that went off met a man');
selection.TypeText(' with a bundle of straw, and said to him: “Please, man, give me that straw to build me a house.”');
selection.TypeText(' Which the man did, and the little pig built a house with it. Presently came along a wolf, and');
selection.TypeText(' knocked at the door, and said: “Little pig, little pig, let me come in.” To which the pig answered:');
selection.TypeText(' “No, no, by the hair of my chiny chin chin.” The wolf then answered to that: “Then I’ll huff,');
selection.TypeText(' and I’ll puff, and I’ll blow your house in.” So he huffed, and he puffed, and he blew his');
selection.TypeText(' house in, and ate up the little pig. The second little pig met a man with a bundle of furze,');
selection.TypeText(' and said: “Please, man, give me that furze to build a house.” Which the man did, and the pig');
selection.TypeText(' built his house. Then along came the wolf, and said: “Little pig, little pig, let me come in.”');
selection.TypeText(' “No, no, by the hair of my chiny chin chin.” “Then I’ll puff, and I’ll huff, and I’ll blow your');
selection.TypeText(' house in. ”So he huffed, and he puffed, and he puffed, and he huffed, and at last he blew the');
selection.TypeText(' house down, and he ate up the little pig. The third little pig met a man with a load of bricks,');
selection.TypeText(' and said: “Please, man, give me those bricks to build a house with. ”So the man gave him the bricks,');
selection.TypeText(' and he built his house with them. So the wolf came, as he did to the other little pigs, and said:');
selection.TypeText(' “Little pig, little pig, let me come in.” “No, no, by the hair of my chiny chin chin.” “Then I’ll huff,');
selection.TypeText(' and I’ll puff, and I’ll blow your house in.” Well, he huffed, and he puffed, and he huffed and he puffed,');
selection.TypeText(' and he puffed and huffed; but he could not get the house down. When he found that he could not, with');
selection.TypeText(' all his huffing and puffing, blow the house down, he said: “Little pig, I know where there is a nice');
selection.TypeText(' field of turnips.” “Where?” said the little pig. “Oh, in Mr. Smith’s Home-field, and if you will be');
selection.TypeText(' ready tomorrow morning I will call for you, and we will go together, and get some for dinner.”');
selection.TypeText(' “Very well,” said the little pig, “I will be ready. What time do you mean to go?” “Oh, at six o’clock.”');
selection.TypeText(' Well, the little pig got up at five, and got the turnips before the wolf came (which he did about six)');
selection.TypeText(' and who said: “Little Pig, are you ready?” The little pig said: “Ready! I have been and come back again,');
selection.TypeText(' and got a nice potful for dinner. ”The wolf felt very angry at this, but thought that he would be up to');
selection.TypeText(' the little pig somehow or other, so he said: “Little pig, I know where there is a nice apple-tree.”');
selection.TypeText(' “Where?” said the pig. “Down at Merry-garden,” replied the wolf, “and if you will not deceive me I will');
selection.TypeText(' come for you, at five o’clock tomorrow and get some apples.” Well, the little pig bustled up the next');
selection.TypeText(' morning at four o’clock, and went off for the apples, hoping to get back before the wolf came; but he');
selection.TypeText(' had further to go, and had to climb the tree, so that just as he was coming down from it, he saw the wolf');
selection.TypeText(' coming, which, as you may suppose, frightened him very much. When the wolf came up he said: “Little pig, what!');
selection.TypeText(' are you here before me? Are they nice apples?” “Yes, very,” said the little pig. “I will throw you down one.”');
selection.TypeText(' And he threw it so far, that, while the wolf was gone to pick it up, the little pig jumped down and ran home.');
selection.TypeText(' The next day the wolf came again, and said to the little pig: “Little pig, there is a fair at Shanklin this');
selection.TypeText(' afternoon, will you go?” “Oh yes,” said the pig, “I will go; what time shall you be ready?” “At three,” said');
selection.TypeText(' the wolf. So the little pig went off before the time as usual, and got to the fair, and bought a butter-churn,');
selection.TypeText(' which he was going home with, when he saw the wolf coming. Then he could not tell what to do. So he got into');
selection.TypeText(' the churn to hide, and by so doing turned it round, and it rolled down the hill with the pig in it, which frightened');
selection.TypeText(' the wolf so much, that he ran home without going to the fair. He went to the little pig’s house, and told him how');
selection.TypeText(' frightened he had been by a great round thing which came down the hill past him. Then the little pig said: “Hah, I');
selection.TypeText(' frightened you, then. I had been to the fair and bought a butter-churn, and when I saw you, I got into it, and');
selection.TypeText(' rolled down the hill.” Then the wolf was very angry indeed, and declared he would eat up the little pig, and that');
selection.TypeText(' he would get down the chimney after him. When the little pig saw what he was about, he hung on the pot full of water,');
selection.TypeText(' and made up a blazing fire, and, just as the wolf was coming down, took off the cover, and in fell the wolf; so the');
selection.TypeText(' little pig put on the cover again in an instant, boiled him up, and ate him for supper, and lived happy ever afterwards.');
selection.TypeParagraph;                    %linebreak
selection.ParagraphFormat.Alignment =0;     %Left-aligned
selection.InsertNewPage;                    %create new Page(cursor moves to new page)

Insert picture

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;

Insert figure to Word

x = 0:pi/100:2*pi;
y = sin(x);
figtoprint=figure(1);                       %opens new figure window
plot(x,y);
title('Title');
xlabel('x-label');
ylabel('y-label');
grid on;
print(figtoprint,'-dmeta');                 %print figure to clipboard
invoke(word.Selection,'Paste');             %paste figure to Word
close Figure 1;

document.SaveAs([pwd '/test.doc']);         %save Document
word.Quit();                                %close Word