-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteToWordFromMatlab.m
323 lines (297 loc) · 14.7 KB
/
WriteToWordFromMatlab.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
function WriteToWordFromMatlab
% -------------------------------------------------------------------
% File: WriteToWordFromMatlab
% Descr: This is an example of how to control MS-Word from Matlab.
% With the subfunctions below it is simple to automatically
% let Matlab create reports in MS-Word.
% This example copies two Matlab figures into MS-Word, writes
% some text and inserts a table.
% Works with MS-Word 2003 at least.
% Created: 2005-11-22 Andreas Karlsson
% History:
% 051122 AK Modification of 'save2word' in Mathworks File Exchange
% 060204 AK Updated with WordSymbol, WordCreateTable and "Flying Start" section
% 060214 AK Pagenumber, font color and TOC added
% -------------------------------------------------------------------
WordFileName='TestDoc.doc';
CurDir=pwd;
FileSpec = fullfile(CurDir,WordFileName);
[ActXWord,WordHandle]=StartWord(FileSpec);
fprintf('Document will be saved in %s\n',FileSpec);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Section 1
%%create header in word
Style='Heading 1'; %NOTE! if you are NOT using an English version of MSWord you get
% an error here. For Swedish installations use 'Rubrik 1'.
TextString='Example of Report Generation from Matlab';
WordText(ActXWord,TextString,Style,[0,2]);%two enters after text
Style='Normal';
TextString='This is a simple example created by Andreas Karlsson, Sweden. ';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
TextString='Updated a sunny day, in February 2006, with -10 ';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
WordSymbol(ActXWord,176);%176 is the degree-symbol
TextString='C. ';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
TextString='The script will just insert a table and two figures into this document. ';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
TextString='Last section is a short introduction for the interested users out there. ';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
TextString='My intention with this demo is to encourage you to let your powerful computer do the';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
TextString=' "monkey-job" such as inserting figures into MS-Word and writing standard reports. ';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
TextString='Hopefully these simple lines will help you getting more time for funny coding';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
TextString=' and problem solving, increasing productivity in other words ';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
TextString='by spending less time in Microsoft Office programs. ';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
TextString='Happy coding!';
WordText(ActXWord,TextString,Style,[1,1]);%enter before and after text
ActXWord.Selection.InsertBreak; %pagebreak
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Section 3
style='Heading 1';
text='Table of Contents';
WordText(ActXWord,text,style,[1,1]);%enter before and after text
WordCreateTOC(ActXWord,1,3);
ActXWord.Selection.InsertBreak; %pagebreak
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Section 3
style='Heading 1';
text='Data From Matlab';
WordText(ActXWord,text,style,[1,1]);%enter before and after text
Style='Heading 2';
TextString='The Self-Explaining Table';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
%the obvious data
DataCell={'Test 1', num2str(0.3) ,'Pass';
'Test 2', num2str(1.8) ,'Fail'};
[NoRows,NoCols]=size(DataCell);
%create table with data from DataCell
WordCreateTable(ActXWord,NoRows,NoCols,DataCell,1);%enter before table
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Section 4
figure;plot([1:10]);title('Figure 1');xlabel('Time [s]');ylabel('Amplitude [A]')
%insert the figure
TextString='First figure';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
FigureIntoWord(ActXWord);
TextString='Second figure';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
figure;plot([1:19],[1:10,9:-1:1]);title('Figure 2');xlabel('Time [s]');ylabel('Amplitude [A]');legend('Signal 1',2)
FigureIntoWord(ActXWord);
ActXWord.Selection.InsertBreak; %pagebreak
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Section 5
Style='Heading 1';
TextString='Flying Start';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
Style='Normal';
TextString='Find out how to do new things in MS-Word by using the "Record Macro"-function ';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
TextString='and look at the Visual Basic commands used.';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
TextString='In Matlab you find the available properties by using get(ActXWord), for top interface,';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
TextString=' and further on with for example get(ActXWord.Selection).';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
TextString='Then find the methods usable from Matlab by using the invoke-function in Matlab ';
WordText(ActXWord,TextString,Style,[0,0]);%no enter
TextString='e.g. invoke(ActXWord.Selection). See the output of that call below. ';
WordText(ActXWord,TextString,Style,[0,1]);%enter after text
TextString='Set a breakpoint here and play around with these commands...';
WordText(ActXWord,TextString,Style,[0,1],'wdColorRed');%red text and enter after text
%Make a long list of some of the methods available in MS-Word
Category='Selection'; % Category='ActiveDocument';
PrintMethods(ActXWord,Category)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%add pagenumbers (0=not on first page)
WordPageNumbers(ActXWord,'wdAlignPageNumberRight');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Last thing is to replace the Table of Contents so all headings are
%included.
%Selection.GoTo What:=wdGoToField, Which:=wdGoToPrevious, Count:=1, Name:= "TOC"
WordGoTo(ActXWord,7,3,1,'TOC',1);%%last 1 to delete the object
WordCreateTOC(ActXWord,1,3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CloseWord(ActXWord,WordHandle,FileSpec);
close all;
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUB-FUNCTIONS
% Creator Andreas Karlsson; [email protected]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [actx_word,word_handle]=StartWord(word_file_p)
% Start an ActiveX session with Word:
actx_word = actxserver('Word.Application');
actx_word.Visible = true;
trace(actx_word.Visible);
if ~exist(word_file_p,'file');
% Create new document:
word_handle = invoke(actx_word.Documents,'Add');
else
% Open existing document:
word_handle = invoke(actx_word.Documents,'Open',word_file_p);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function WordGoTo(actx_word_p,what_p,which_p,count_p,name_p,delete_p)
%Selection.GoTo(What,Which,Count,Name)
actx_word_p.Selection.GoTo(what_p,which_p,count_p,name_p);
if(delete_p)
actx_word_p.Selection.Delete;
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function WordCreateTOC(actx_word_p,upper_heading_p,lower_heading_p)
% With ActiveDocument
% .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
% True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
% LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
% UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
% True
% .TablesOfContents(1).TabLeader = wdTabLeaderDots
% .TablesOfContents.Format = wdIndexIndent
% End With
actx_word_p.ActiveDocument.TablesOfContents.Add(actx_word_p.Selection.Range,1,...
upper_heading_p,lower_heading_p);
actx_word_p.Selection.TypeParagraph; %enter
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function WordText(actx_word_p,text_p,style_p,enters_p,color_p)
%VB Macro
%Selection.TypeText Text:="Test!"
%in Matlab
%set(word.Selection,'Text','test');
%this also works
%word.Selection.TypeText('This is a test');
if(enters_p(1))
actx_word_p.Selection.TypeParagraph; %enter
end
actx_word_p.Selection.Style = style_p;
if(nargin == 5)%check to see if color_p is defined
actx_word_p.Selection.Font.Color=color_p;
end
actx_word_p.Selection.TypeText(text_p);
actx_word_p.Selection.Font.Color='wdColorAutomatic';%set back to default color
for k=1:enters_p(2)
actx_word_p.Selection.TypeParagraph; %enter
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function WordSymbol(actx_word_p,symbol_int_p)
% symbol_int_p holds an integer representing a symbol,
% the integer can be found in MSWord's insert->symbol window
% 176 = degree symbol
actx_word_p.Selection.InsertSymbol(symbol_int_p);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function WordCreateTable(actx_word_p,nr_rows_p,nr_cols_p,data_cell_p,enter_p)
%Add a table which auto fits cell's size to contents
if(enter_p(1))
actx_word_p.Selection.TypeParagraph; %enter
end
%create the table
%Add = handle Add(handle, handle, int32, int32, Variant(Optional))
actx_word_p.ActiveDocument.Tables.Add(actx_word_p.Selection.Range,nr_rows_p,nr_cols_p,1,1);
%Hard-coded optionals
%first 1 same as DefaultTableBehavior:=wdWord9TableBehavior
%last 1 same as AutoFitBehavior:= wdAutoFitContent
%write the data into the table
for r=1:nr_rows_p
for c=1:nr_cols_p
%write data into current cell
WordText(actx_word_p,data_cell_p{r,c},'Normal',[0,0]);
if(r*c==nr_rows_p*nr_cols_p)
%we are done, leave the table
actx_word_p.Selection.MoveDown;
else%move on to next cell
actx_word_p.Selection.MoveRight;
end
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function WordPageNumbers(actx_word_p,align_p)
%make sure the window isn't split
if (~strcmp(actx_word_p.ActiveWindow.View.SplitSpecial,'wdPaneNone'))
actx_word_p.Panes(2).Close;
end
%make sure we are in printview
if (strcmp(actx_word_p.ActiveWindow.ActivePane.View.Type,'wdNormalView') | ...
strcmp(actx_word_p.ActiveWindow.ActivePane.View.Type,'wdOutlineView'))
actx_word_p.ActiveWindow.ActivePane.View.Type ='wdPrintView';
end
%view the headers-footers
actx_word_p.ActiveWindow.ActivePane.View.SeekView='wdSeekCurrentPageHeader';
if actx_word_p.Selection.HeaderFooter.IsHeader
actx_word_p.ActiveWindow.ActivePane.View.SeekView='wdSeekCurrentPageFooter';
else
actx_word_p.ActiveWindow.ActivePane.View.SeekView='wdSeekCurrentPageHeader';
end
%now add the pagenumbers 0->don't display any pagenumber on first page
actx_word_p.Selection.HeaderFooter.PageNumbers.Add(align_p,0);
actx_word_p.ActiveWindow.ActivePane.View.SeekView='wdSeekMainDocument';
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function PrintMethods(actx_word_p,category_p)
style='Heading 3';
text=strcat(category_p,'-methods');
WordText(actx_word_p,text,style,[1,1]);
style='Normal';
text=strcat('Methods called from Matlab as: ActXWord.',category_p,'.MethodName(xxx)');
WordText(actx_word_p,text,style,[0,0]);
text='Ignore the first parameter "handle". ';
WordText(actx_word_p,text,style,[1,3]);
MethodsStruct=eval(['invoke(actx_word_p.' category_p ')']);
MethodsCell=struct2cell(MethodsStruct);
NrOfFcns=length(MethodsCell);
for i=1:NrOfFcns
MethodString=MethodsCell{i};
WordText(actx_word_p,MethodString,style,[0,1]);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function FigureIntoWord(actx_word_p)
% Capture current figure/model into clipboard:
print -dmeta
% Find end of document and make it the insertion point:
end_of_doc = get(actx_word_p.activedocument.content,'end');
set(actx_word_p.application.selection,'Start',end_of_doc);
set(actx_word_p.application.selection,'End',end_of_doc);
% Paste the contents of the Clipboard:
%also works Paste(ActXWord.Selection)
invoke(actx_word_p.Selection,'Paste');
actx_word_p.Selection.TypeParagraph; %enter
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function CloseWord(actx_word_p,word_handle_p,word_file_p)
if ~exist(word_file_p,'file')
% Save file as new:
invoke(word_handle_p,'SaveAs',word_file_p,1);
else
% Save existing file:
invoke(word_handle_p,'Save');
end
% Close the word window:
invoke(word_handle_p,'Close');
% Quit MS Word
invoke(actx_word_p,'Quit');
% Close Word and terminate ActiveX:
delete(actx_word_p);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%