File Exchange

image thumbnail

WriteToWordFromMatl​ab

version 1.3 (15 KB) by

Automatically create MS Word documents from Matlab with figures, tables, headers and text.

4.57576
37 Ratings

64 Downloads

Updated

View License

This m-file is a modification of the 'save2word'. It's an example of how to automatically create MS Word documents from MATLAB with figures, tables, headers and text.
The different basic functionalities to add text with different style and to add figures are broken into sub-functions that can be used in a simple way.

Comments and Ratings (46)

Calabrese

This is fantastic, is there a way to open an existing document and search to see if a phrase is present or not?

Hi, Can someone help me with the following error message in M2015b. Thanks

Undefined function or variable 'TablesOfContents'.

Error in WriteToWordFromMatlab>WordCreateTOC (line 185)
    actx_word_p.ActiveDocument.TablesOfContents.Add(actx_word_p.Selection.Range,1,...

Error in WriteToWordFromMatlab (line 68)
    WordCreateTOC(ActXWord,1,3);

Error in run (line 96)
evalin('caller', [script ';']);

Andreas Karlsson

Hi,
Have you changed the names of the styles to fit your (French?) installation of MSWord?
Look for this in the code (and all other places where Style= is defined:
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'.

Thomas

Thomas (view profile)

Hello, Can someone help me with the following error message in M2012b. Thanks
.
K>> actx_word_p.Selection.Style = style_p;
Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: L'élément dont le nom est spécifié n'existe pas.
Help File: wdmain11.chm
Help Context ID: 62e2

Leo-Jay Black

Dear Andreas,

thank you very much for sharing your Matlab code with the rest of the community.

I have used your functions many times and they are great.

I was wondering if you could paste and entire table with data instead of writing it cell by cell. The moveright command in VB is very slow and I thought about to use the

    Selection.PasteExcelTable False, True, True
    Selection.Tables(1).Style = "Grid Table 5 Dark - Accent 1"

PasteExcel Table command in MS Word.

The VB macro works in MS Word but I struggle to get matlab to execute the above commands.

Any advise from you side will be appreciated.

I have written the code that pastes the data table into MS Excel and I can copy the table into the clipboard.

I just struggle with the last bit.

KR

Jens

For german Heading is replaced by Überschrift and Normal to Standard

Hi, please i need run the script in spanish word version. How I modify the code to run it?
thanks.

I have used this file many times for my simple reports. I appreciate your work.

Does anyone have a way to include a tab in the text.

Recording the macro yields
Selection.TypeText Text:=vbTab

But how do I get the vbTab command into
actx_word_p.Selection.TypeText

Bogdana

Really nice structure with well commented the code.

Can anybody help me. I have problems merging the cells.

Word help says:
 myrow.Cells.Merge

I want to merge first row in the table, but:
actx_word_p.Selection.Rows(1).Cells.Merge
dosn't work

it returns error:
No appropriate method, property, or field Cells for class
Interface.0002094C_0000_0000_C000_000000000046.

Any idea? Thanks

Eddie

Eddie (view profile)

I just upgraded my PC to word 2010. I had to change wdColorAutomatic to wdAudo etc and use ColorIndex instead of color. This is the link where I found the new constants
http://msdn.microsoft.com/en-us/library/office/aa193509(v=office.10).aspx

Eddie

Eddie (view profile)

Works great with Matlab 2011b and Word 2007
Many thanks!

Gregory

Still works! Minor changes were needed -
 
In WordText: (changed to ColorIndex instead of Color)
actx_word_p.Selection.Font.ColorIndex='wdAuto';%set back to default color

actx_word_p.Selection.Font.ColorIndex=color_p;

In WordCreateTOC: (added a 0 to the end) actx_word_p.ActiveDocument.TablesOfContents.Add(actx_word_p.Selection.Range,1,upper_heading_p,lower_heading_p,0);

Also, Added this to the beginning, more as a reminder to myself:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% To see the full command list:
%
% 1) Open Word and go to the "Help" option
% 2) Click on "Microsoft Office Help" and wait for the window to pop up
% 3) Click the arrow next to "Search" to bring down a menu
% 4) At the bottom, click "Developer Reference"
% 5) "Word Object Model Reference" contains all of the available objects
% that can be used, and "Enumerations" (at the bottom, only thing not
% in alphabetical order) tells you wd constants that are available.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Rogier

Rogier (view profile)

For use with Word2010, I removed/commented the following rule in function WordText:

actx_word_p.Selection.Font.Color='wdColorAutomatic';%set back to default color

Works great and saves lots of time.

Rogier

Rogier (view profile)

Bernd

Bernd (view profile)

very nice structure of code and easy to understand and use. However the hard coded format styles for headings and text styles make it hard to ease automatic doc generation for our multi-cultural office environments ;)
...however I cannot blame to the author of this code for this!

Andreas Karlsson

Adam, try this.
ActXWord.Selection.InsertBreak(3);

It can be found if you look into the list of selection-methods created by the script in its TestDoc.doc

Here is a link to the wdBreak enumeration
http://msdn.microsoft.com/en-us/library/office/ff195905.aspx

Adam

Adam (view profile)

This is really good, but I'm experiencing a problem inserting a section break into word.

I've recorded a macro, which used the VB command:
Selection.InsertBreak Type:=wdSectionBreakContinuous

However, my Matlab version :
set(actx_word_p.Selection.InsertBreak,'Type','wdSectionBreakContinuous')

returns an error "One or more output arguments not assigned during call to "InsertBreak". "

Can you see something obvious that I'm missing?

Greg Imamura

Sorry about multiple submissions.
Actxserver object had trouble creating files that do not already exist because of trouble/corruption with MAPI32.dll, which is called from Outlook, not Word.? If the file already exists (e.g. TestDoc.doc), it worked no problem.
I have not tried fixing MAPI32.dll, but that may solve problem.

Ran

Ran (view profile)

it doesn't work with office 10
this is the error i receive
??? 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 ==> WriteToWordFromMatlab>WordText at 198
actx_word_p.Selection.Style = style_p;

Error in ==> WriteToWordFromMatlab at 29
    WordText(ActXWord,TextString,Style,[0,2]);%two enters after text

Santosh

Andreas Karlsson

In here again checking out what happened during the years. THANKS for all the nice comments!
For Mr. Dirk Z I can say that you should open the file and read the comments. That will give you the hint of doing a search/replace - easy. Read Dimitri Gabris comment from 11 Apr 2006...
After a lot of years as a Matlab/Simulink-programmer I'm now a consultant programming in other languages. Missing the power of Matlab.
If you need help with some Matlab/Simulink coding I would be glad to help...

benharper1977

thank you very much Andreas, your script saves me a lot of time in doing copy&paste with many figures...you're a hero to me.

Karel Macek

Very usefull.

Yuval Ben-Dov

Great functionality, easy to follow, thanks for sharing!

Dirk Z

really a good script.
The problem is that the Goto function with wdGoToBookmark as target does not work.
Can anyone help?

B M

only works in english office installations...
so you need to do a lot of hard-coding. On my pc i have a dutch word, my boss an english word .. not easy

Tobias C

Maybe the most useful script at Matlab central. Well written code with good comments.

Thorbjorn Knudsen

gr8! thx

T

Ramon Planas

Great code!! Thanks very much

c r

Your code maved me lots of time. Thanks!.

E B

Joseph nonereally

This is a great piece of example code. Thank you Andreas.

Jiabei Yuan

If there is a rating over 6, I'll never rate this code with 5!!!

Axel Kiefer

I fully agree with prevoius raters. Andreas work is EXCELLENT. It saved a lot of time.

Dimitri Garbi

That is exactly what I was looking for. The code allow to generate generic reports automatically. Thank you !

note : Styles must be adapted in order to have no error. 'Heading 1' in english correspond to 'Rubrik 1' in swedish (as commented) and to 'Titre 1' in french.

Theo Venter

This is excellent. Thanks

E TK

Very nice, easy to use and intuitive interface. It does exactly what you would expect. Thanks for posting!

Updates

1.3

Changed calls to color-method into colorIndex to support MS-Word 2013. Thanks Martin van de Ven!

1.0

Added BSD license upon request from Mathworks

MATLAB Release
MATLAB 6.5.1 (R13SP1)
Acknowledgements

Inspired by: saveppt, save2word

Inspired: wordreport

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video