View License

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

» Watch video

Highlights from
Control MS Word from matlab

4.5 | 4 ratings Rate this file 41 Downloads (last 30 days) File Size: 248 KB File ID: #28558 Version: 1.4

Control MS Word from matlab



26 Aug 2010 (Updated )

A set of functions that can control word from matlab by activex.

| Watch this File

File Information

You can open word documents, add text, newline, pagebreaks, alter color, fonts, add pictures, tables and captions, etc. This project is not finished, but I don`t have much time anymore to work on it. I`m posting it here in case someone finds it useful and maybe also want to try to finish the project. The main job is done, but the functions needs to be tested properly, and some functionality is missing.

Required Products MATLAB
MATLAB release MATLAB 8.3 (R2014a)
MATLAB Search Path
Other requirements Windows
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (28)
26 May 2016 Ivar Eskerud Smith

Hi Harald, the saveAs function was indeed missing, I have updated the package now. It also contains some new functions. To save a Word document either call save and then close, or saveAs and then close.
The Word class however should work fine, you need to add the folder that contains the @Word folder to your matlab path. It works for me on several computers on both old and new versions of matlab.
Regards, Ivar

Comment only
20 May 2016 Harald Berndt

Nice attempt, but it seems to have issues:
(1) the folder "@Word" does not act like an abject folder (because no class is defined there?), so I wouldn't know how to install it (I won't put it all on the path since, e.g., the "close" function shadows a built-n function)
(2) saveAs is indeed missing, and I find no way to save a file and close the ActX object safely.

Comment only
30 Oct 2014 Charles Harrison

Nevermind, I answered my own question. I didnt initially see that the values are defined under the enumerations section in Microsoft's API.

Comment only
30 Oct 2014 Charles Harrison

Ivar, I have a couple general questions as to how you actually came up with some of your code.

How did you figure out what MATLAB arguments need to be inputted into a given VBA method.

For instance, in your goTo function, to executed:
actxWord.Selection.GoTo(what,which,count,name) how did you find out that wdGoToField corresponds to a what of 7?

Comment only
26 Jun 2014 Micky Harris

One more question. I don't see a saveAs in the Word/@Word folder. Is this a missing file? I have the save function but not save as.

Comment only
26 Jun 2014 Micky Harris

Does anyone have a simple example of how to create a report with Table of contents, a figure, a table, headings and text? Also how to change page layout to landscape would be really nice too.

Comment only
14 May 2014 Ivar Eskerud Smith

ashkaan, the scalePicture function takes a handle to a picture as input, for instance returned from the insertPicture function. If you see inside the insertPicture function you can see an example.

Comment only
13 May 2014 ashkaan

Does anyone know how to use scalePicture function ?

Comment only
12 Nov 2013 Gregory

Thank you very much for this tool!

Some comments from my side:
1. Function scaleAllImages, line 163-164:
There is no need in specifying both ScaleHeight and ScaleWidth. Indeed, the aspect ratio should be hold, so I replaced these lines with


2. If somebody wants to insert Matlab figs into the MS word documents as is (as they are presented at the screen), the following code should be helpful, I think:

%additional arguments specified to avoid opening the figure that is already opened
fig = openfig(file,'reuse','invisible');
%to set the figure size in the word document to be the same as on the screen
%copy figure to clipboard as windows EMF

I've placed this code to the insertPicture function instead of the line 413:
actxWord.Application.Selection.InlineShapes.AddPicture( file, link, savewithdoc );

02 Mar 2013 Ivar Eskerud Smith

Ian Philips: I do not understand why you get that error with the saveAs function, are you sure the word document was not closed before calling that function? Then the wordHandle in the object will be invalid. The insertTable function takes a cell array as input, which can contain both string and numbers. If I get the time I might try to add some more documentation and examples

Comment only
25 Jan 2013 Ian Phillips

I am using Matlab R2009a (Version 7.8) with MS Word 2010.
In general an excellent library for controlling MS Word, the only problem is the lack of examples and documentation.
The example you posted on the 15th June 2012 worked great except for the command "saveAs(w,file);" which returns the following error:

??? Error using ==> saveas at 59
Invalid handle.

Error in ==> dir_test3 at 100

Any suggestions?

Please can you also post an example that uses the scalePicture and insertTable functions? In particular, what is the format of the data passed into the insertTable function?

Thanks again for the great library.

16 Jan 2013 Ivar Eskerud Smith

I will have to see if I can reproduce the error. Have you moved any files, for instance the Word.m file out of the @Word folder?

Comment only
15 Jan 2013 Peter

Peter (view profile)

Hi Ivar,

I get the same error as Karthik KJ. Has this been resolved?

I'm using Matlab R2012b and MS Word 2012.

Hope very much to get this fixed! :)


Comment only
08 Sep 2012 Thenappan

08 Sep 2012 Thenappan

Hi ivar,
where can i have these must be very useful for my project

Comment only
17 Jul 2012 Ivar Eskerud Smith

Karthik: I have only tried using Word 2010, and that works fine for me....

Comment only
25 Jun 2012 Karthik KJ

Hi Ivar,

I could able to run now, but i have to use older version of MS word(2003). Did you added the capability to work it in MS word 2010

Comment only
19 Jun 2012 Ivar Eskerud Smith

Have you added all folders regarding this Word class to your path? What version of matlab are you using?

Comment only
19 Jun 2012 Karthik KJ

Hi Ivar,
I have just used the your example
file='c:\Users\Public\Documents\IBM\test.docx'; %use full path

Comment only
18 Jun 2012 Ivar Eskerud Smith

Erosoft: Replace this.actxWord.Selection.Range.Bookmarks with this.actxWord.ActiveDocument.Bookmarks. That worked for me. If you use Selection.Range, I think you must make sure you have selected a range that includes your bookmark. When using ActiveDocument I think you look for bookmarks in the entire document.

Karthik KJ: Can you show me the code you use which produces that error message?

Comment only
18 Jun 2012 Karthik KJ

I am getting this error when i try to open the word document

Error using class
The CLASS function must be called from a class constructor.

Error in Word (line 29)
this = class( this, 'Word' );

Comment only
18 Jun 2012 Erosoft


I have done these 2 functions for bookmark :

function addBookmark( this,bookname )

set( this.actxWord.Selection.Range.Bookmarks.Add(bookname));

function s = getBookmarkItem( this,bookname )

s=get( this.actxWord.Selection.Range.Bookmarks.Item(bookname));

They work quite well, when I create through Matlab a new bookmark, it appears in word. And I can retrieve the property of this bookmark.

But if a bookmark was previously created in word, I can't retrieve its parameters, because it doesn't exist.
Further more if I save and close the document and open back with matlab, the bookmark created with Matlab is still here but I can't retrieve its parameters.

I do not think I made a mistake. Is it an office or windows error with a library?

Comment only
15 Jun 2012 Ivar Eskerud Smith

% I should maybe have mentioned that this class requires the "getarg" function:
% I`ll try to update it with some examples when I get the time. Short example:
file='C:\Users\ies\Documents\MATLAB\test.docx'; %use full path
w=Word(file,true); %create document and make visible

addText(w,'Introduction','Heading 1',2); %create a heading, 2 spaces after
addText(w,'Some text....'); %add some text
newline(w,6); %6 newlines
insertCaption(w,' A nice figure');


Comment only
15 Jun 2012 Erosoft

When I use this function it works, but I precise the whole directory 'c:\...\test1.doc'

Comment only
14 Jun 2012 Arpit Kothari

Thanks for the upload but I'm not being able to use this function.
The way I tried was:
Word('test1.doc',1) %Initialize MSWord

It returns an error:
Undefined function or method 'getarg' for input arguments of type 'cell'.
I'm using Matlab R2010a. Do I need to write this function?

Comment only
08 May 2012 Amr Sadek

Can you explain how it works?

Comment only
22 Feb 2012 Erosoft

01 Sep 2010 Ivar Eskerud Smith

Please tell me/post if anyone has made any progress on this!

Comment only
23 Feb 2012 1.1

Renamed and improved some functions, added a few new, like insertTOC, insertTable, scalePicture, clear, copy, paste

17 Jan 2013 1.2

Removed wrong tag

26 May 2016 1.3

Added some functions: saveAs, getCaptions, goToEndOfDocument, numberOfLines, goToNextLine, goToPreviousLine, selectNextLine, selectPreviousLine, copyFigureCaptionsToEnd and copyTablesAndTableCaptionsToEnd

03 Oct 2016 1.4

Seems something might have gone wrong last time I tried to update. This version should include some new functions, among other saveAs.

Contact us