How can I set landscape and margins in word using matlab activex

4 vues (au cours des 30 derniers jours)
Andrew Tilmouth
Andrew Tilmouth le 1 Nov 2012
Commenté : Jeff Shi le 15 Sep 2015
I am using writetoword.m from the matlab file exchange to print figures and text to a word document but I would also like to make the document landscape orientation and change the right and left margins to 1cm as well.
I tried adding these lines of code at the beginning of the wrtietoword.m file after it creates the word document:-
This line gives no errors but does not seem to do anything and does not change the orientation to lanscape:- actXword.ActiveDocument.Pagesetup.Orientation = 'wdOrientLandscape';
Thesee lines give an error actXword.ActiveDocument.Pagesetup.LeftMargin = 'CentimetersToPoints(1)'; actXword.ActiveDocument.Pagesetup.RightMargin = 'CentimetersToPoints(1)';
Can anyone hep with the right syntax. Is there a good guide anywhere for the matlab activex syntax versions of VBA commands ?
thanks Andrew

Réponse acceptée

Eric
Eric le 1 Nov 2012
Modifié(e) : Eric le 1 Nov 2012
wdOrientLandscape is part of an enumeration. It's value is actually 1.
The proper usage for CentimetersToPoints should be
actXword.CentimetersToPoints(1)
but this gives an error. I'm not sure why. However, this is easy enough to set without calling the function as 1 cm is equal to 28.3464567 points.
I'm not sure of a good guide. The command structure for the COM interface to Word is quite similar to the VBA commands - you just have to be careful. Some thoughts that come to mind:
1. You can never assume that the root object is known. For example, in VBA you generally don't have to supply the base "Application" argument. This is why you would need to supply actXword.CentimetersToPoints rather than just CentimetersToPoints when calling the function from Matlab.
2. You have to be careful with collections and how you index into them. In VBA you can often supply a text argument to index into a collection (e.g., reference a worksheet by its name in Excel). This does not work in the COM interface. You have to index into the Item property of the collection.
3. You also have to be careful with enumerations (see above). To get enumeration values, open the VBA editor and go to the Immediate window. To get the value for an enumeration type
?enumerationName
This will show you the actual numerical value to use. You can also look these up on the web.
4. A useful function in Matlab is methodsview(). This can give you information about what methods an object supports.
Good luck,
Eric

Plus de réponses (1)

Andrew Tilmouth
Andrew Tilmouth le 2 Nov 2012
Thanks Eric
so the following lines of code work correctly in word 2010 at least
To set the left and right margins to 1cm:-ActXWord.ActiveDocument.PageSetup.LeftMargin = 28.3464567 ; ActXWord.ActiveDocument.PageSetup.RightMargin = 28.3464567;
To set the doc to landscape:- (because wdOrientLandscape enum = 1) ActXWord.ActiveDocument.PageSetup.Orientation = 1;
Andrew

Catégories

En savoir plus sur Use COM Objects in MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by