File Exchange

image thumbnail


version (34.2 KB) by Laurent Vaylet
A Word report generator (ActiveX-based) in an object-oriented way


Updated 06 Jul 2009

View Version History

View License

WORDREPORT creates or opens an existing Microsoft Office Word report and provides helper functions to add some content: text, figures, Simulink models, Stateflow charts and much more. It also helps in adding or updating the table of contents, in setting page numbering or in finding text. Actually, it is possible to mimic almost everything you can do manually. Just record a macro in Word and analyze the generated VBA code to find out how to use the ActiveX technology.

Example: create a new document called 'Foo.doc' and add some content (headings, figures, page breaks, page numbers, table of contents)

reportFilename = fullfile(pwd,'foo.doc');
wr = wordreport(reportFilename);
wr.setstyle('Heading 1');
wr.addtext('TOC', [1 1]); line break before and after text
wr.createtoc(1, 3);
wr.setstyle('Heading 1');
wr.addtext('MATLAB data', [1 1]); line break before and after text
wr.setstyle('Heading 2');
wr.addtext('Sample table', [0 1]); line break after text
dataCell = { ...
'Test 1', num2str(0.3) , 'OK'; ...
'Test 2', num2str(1.8) , 'KO'};
[nbRows, nbCols] = size(dataCell);
wr.addtable(nbRows, nbCols, dataCell, [1 1]); line break before table
wr.setstyle('Heading 2');
wr.addtext('Sample figure', [0 1]); line break after text
figure; plot(1:10);
title('Figure 1'); xlabel('Temps [s]'); ylabel('Amplitude [A]');

Do not hesitate to ask for more help and more advanced examples, especially when dealing with screenshots of Simulink models and Stateflow charts. I'll be happy to be of any assistance !

Cite As

Laurent Vaylet (2021). wordreport (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (26)

Lenin Cruz

Hello, does this use the Report Generator toolbox?


Can you describe how to add the appendix letter to the figure caption, in the appendix, and also make a table of contents for the figures, in the appendix. i.e. change from "Figure 54. text" to "Figure A - 54. text", when the appendix is in a separate document.

youssouf outgda

Amazing Work, Thanks


Mustafa Mian

When I close my document, I export it as a pdf file using
wdFormatPDF = 17;
invoke(hdlWordDoc, 'SaveAs2', PDFFilename, wdFormatPDF);
When exporting as pdf, how do I ensure that bookmarks are placed for heading in the pdf file.
there is an option that you need to check while exporting to do this. How do I use MATLAB to ensure that option is selected?


Currently using Office 2016/Matlab R2016b. The demo runs into a few errors and I was wondering if anyone else had this issues. Specifically I get the following errors

Warning: Resetting generic name for heading styles to default 'Heading '
> In wordreport_demo (line 47)
The 'Color' property name is ambiguous in the '00020952_0000_0000_C000_000000000046' class.

Error in wordreport/AddText (line 192)
hdlActiveX.Selection.Font.Color = 'wdColorAutomatic'; % Set back to default color

Error in wordreport_demo (line 51)
wr.addtext(textString, [0 2]); % two line breaks after text

Tony L.

Hi Osama,
I'm new to this function, but your issue might have to do with the Style of the line/location where you're pasting the figure. If that style is a Heading then it will be included in the TOC (as long as it's within the specified min/max parameters.

I have an unrelated issue with the TOC lines not being hyperlinks. "UseHyperlinks" should be True by default but the TOC created via this function is not hyperlinked. Any help is appreciated, in the meantime I'll keep digging.

Osama Maqbool

Hi.Thanks a lot for this tool, made my life easier! I have a problem. The update table of contents function behaves oddly. Instead of a table of contents in the beginning, I get plots in between the lines of the table of contents. The same problem occurred when I included some plots in the demo version. Can anyone help me?


Thanks for this useful tool. I have numerous daily reports that use this function. However, 2015b has numerous issues. Am going to try and get MW to help.


Ran, Deborah, James and others:
The true reason is the specified style does not exist in Word file.

Change the style (headingString) in demo, or create one in Word file.


To make the figure in the Word document have same height/width as on screen: set(gcf,'PaperPositionMode','auto')


I used the addfigure function. It seems to work. I tweaked it to accept a handle (this way I don't have to worry about selecting it first) and set the style as normal. Works great.

Laurent: Thanks for this. I've been trying to learn how to manipulate word docs and your examples made it extremely easy to understand. There were some errors for office 2010 (i.e. color property doesn't exist.) You need to use ".Selection.Font.TextColor.RGB" instead.


Very useful job.
I just have a problem to paste figure. The function .addfigure seems to do nothing. Does anybody have the same problem ? (matlab version : 2014a, word 2010)


Ran, Deborah and others, I had this problem but it seemed to go away when using absolute file paths to the Word document. I used to get the absolute paths.


I get the same error as Ran. Is the author still supporting this??


Has anyone tested the GoTo method ?

For some reason, when I try to goto an exsiting bookmark, I get:

Invoke Error, Dispatch Exception:


doen't work with my office 10
this is the error i receive
Warning: Resetting generic name for heading styles to default 'Heading '
> In wordreport_demo at 47
??? Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: ???? ??? ????? ???? ????.
Help File: C:\Program Files\Microsoft Office\Office10\1037\wdmain10.chm
Help Context ID: 62e2

Error in ==> wordreport>AddText at 187
hdlActiveX.Selection.Style = currentStyle;

Error in ==> wordreport_demo at 51
wr.addtext(textString, [0 2]); % two line breaks after text


Fabulous, thanks. How would I apply Word's built-in table formatting, such as Selection.Tables(1).Style = "Light Shading" ?

Romain Gar

Good work, i have just one question : I already made a Simulink Report Generator. But my work is to put my Simulink Report into an old existing Word Document, which contains some informations not coming from simulink model. I want to put into this WordDocument my Report in certain section.
Can you help me?

Moises enrique

Excellent work!!! Two questions 1) Can an example modified to include a proper report format with title page and diffent color and size fonts. 2) The the processing be done in the back ground without openning anything? Thanks a lot.

Gernot Druml

Thanks, it makes my life realy easier.
Especially the functionality to insert on an selected place of word helps me in the daily work

Laurent Vaylet


1) The plugins system you mention would be ideal to ensure maximal power and modularity. This kind of contribution could greatly benefit from such an architecture. It's also true that i have a few ideas on how to rewrite the code to accept and automatically detect subfunctions. However, it's a time-consuming task so i can't promise i'm going to implement these features :-( Right now, i prefer to concentrate on a full demo of all current possibilities.

2) I could certainly set up this kind of structure if i was hired by the Mathworks but unfortunately it is not the case :-) I already have a full-time job and a rest-of-the-time girlfriend that both imply not-too-much-remaining-time for a thread about a subject as complex as ActiveX.

WORDREPORT and all of its contents are "open-source" so feel free (as in free speech and free beer) to update/expand/modify it if you realize i'm no longer maintaining it. As you said, it's just the beginning. There are endless possibilities to what you can do with ActiveX !

Thanks again for your suggestions and see you soon for a full demo (and maybe more...).


as you point o Meldolesi


Good job.

It is simple and works well.

I have a couple of suggestions:

1) as you point out this tool is destined to grow. A limitation to the growt is the fact that if WORDREPORT is all contained into a simgle file it would require editing to merge different "subfunctions" posted by different people.
It would be good for people to be able to post separate "subfunctions" on to the mathworks website and for people to be able to download them and plug them in by copying them into a directory, without editing WORDREPORT. This means that the main function WORDREPORT would have to have a function which updates the links to the external subfunctions.

I can see this working better.

2) Have you considered starting a forum on the Mathworks website as a dpository for the plugin and general discussions on how to use activeX?


Laurent Vaylet


Thanks for your congratulations and constructive remarks. To answer them:

0 - I'm currently working on externalizing a full demo including Simulink/Stateflow screenshots. Hope to release it by the end of the week, depending on my workload.

1 - I'm afraid I won't be able to speed things up :-( Execution speed must be limited by the ActiveX technology itself. Someone has any idea on the subject ?

2 - Actually, Word is opened once at the beginning then hidden during the process of adding content. However, it may be possible to play with the Visible property and the Trace method (in CreateDoc) to completely hide the process and prevent user from taking control.

3 - To override this 'append-by-default' behavior, you can check for file existence and then decide whether to keep the file or not. You can also use the GOTO method to jump to a specified item of the document (table, heading, figure, line, section, ...).

4 - I agree, there's plenty of room for improvement on this part :-)

5 - Default settings for line breaks is [0 1] (one after and none before) so you can get rif of this argument most of the time. However, your linebreak method is not a bad idea. To investigate too, maybe in the next version !

See you later for the full demo pack,


Dimitri Shvorob

Great job! This is definitely more convenient than the predecessor, Andreas Karlsson's 'wordtext'. I wonder if there are functional improvements as well - could the author expand the demo, setting it up as a proper m-file?

Here are a few wrinkles that do not detract from Laurent's accomplishment.

1. The process is relatively slow: the demo took 15 seconds to run.

2. Word is opened, closed and re-opened; could the document creation proceed 'behind -the-scenes'?

3. 'Append-by-default' behavior: if the target document exists, it will be added to.

4. Table output is very cumbersome and cries out for improvement.

5. Surely it's possible to dispense with the '[0, 0]'s, '[0 1]'s and '[1 1]'s by introducing a line-break method?

CAE support

Good job! As said a friend close to me: "c'est guenial!".

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: WriteToWordFromMatlab

Community Treasure Hunt

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

Start Hunting!