File Exchange

image thumbnail

Control MS Word from matlab

version 1.4 (248 KB) by

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



View License

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.

Comments and Ratings (33)


Jacob (view profile)

I don't know about the tab key, but I figured out how to create a right arrow key... Just create a new function like this:

function goToRight( this )



You can do that for any direction.



All, is there any way to command Microsoft word to hit the tab key or right arrow key? I am trying to edit a table that already exists in a Microsoft word doc. Also, how can I select text in the header and footer of a word doc?

Amish, please see the documentation in the file, it is described there. Type "help copyTablesAndTableCaptionsToEnd".

Amish Ghadiya

Hello All,
I want to use 'copyTablesAndTableCaptionsToEnd' function, For that function inputs arguments are required (this, captionAbove, chapterText,chapterStyle)

Please guide me how to use this function !

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

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.

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

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?

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.

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.

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.


Does anyone know how to use scalePicture function ?


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 );

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

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.

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?


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! :)




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

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

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

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

Karthik KJ

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

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?

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' );



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?

% 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');



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

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?

Amr Sadek

Can you explain how it works?


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



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


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


Removed wrong tag


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

MATLAB Release
MATLAB 8.3 (R2014a)

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

» Watch video