Code covered by the BSD License  

Highlights from
Control MS Word from matlab

4.5

4.5 | 4 ratings Rate this file 71 Downloads (last 30 days) File Size: 243 KB File ID: #28558

Control MS Word from matlab

by

 

26 Aug 2010 (Updated )

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

| Watch this File

File Information
Description

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 7.13 (R2011b)
Other requirements Windows
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (26)
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.

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?

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.

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.

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.

13 May 2014 ashkaan

Does anyone know how to use scalePicture function ?

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

set(shapes.Item(i),'LockAspectRatio','MsoTrue');
set(shapes.Item(i),'ScaleHeight',factor);

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
set(gcf,'PaperPositionMode','auto');
%copy figure to clipboard as windows EMF
print(fig,'-dmeta');
actxWord.Application.Selection.Paste();
close(fig);

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

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
saveAs(w,file);

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?

15 Jan 2013 Peter

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

-Peter

08 Sep 2012 Thenappan  
08 Sep 2012 Thenappan

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

17 Jul 2012 Ivar Eskerud Smith

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

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

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?

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
w=Word(file,true);

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?

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

18 Jun 2012 Erosoft

Hello,

I have done these 2 functions for bookmark :

function addBookmark( this,bookname )

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

function s = getBookmarkItem( this,bookname )

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

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?

15 Jun 2012 Ivar Eskerud Smith

% I should maybe have mentioned that this class requires the "getarg" function:
% http://www.mathworks.com/matlabcentral/fileexchange/35415-getarg
% 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
[pth,name,ext]=fileparts(file);
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
pic=sprintf('%s/fig.png',pth);
f=figure;plot(1,1,'*');print(f,'-dpng',pic);close(f);
insertPicture(w,pic);
insertCaption(w,' A nice figure');
saveAs(w,file);

-Ivar

15 Jun 2012 Erosoft

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

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

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?

08 May 2012 Amr Sadek

Can you explain how it works?

22 Feb 2012 Erosoft  
01 Sep 2010 Ivar Eskerud Smith

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

Updates
23 Feb 2012

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

17 Jan 2013

Removed wrong tag

Contact us