Add custom property to word via actxserver
9 views (last 30 days)
Show older comments
I want to add a new custom document property to a word document
This word macro works totally fine:
ActiveDocument.CustomDocumentProperties.Add _
Name:="NewProp", _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="NewValue"
Nevertheless, when I try to use the actxserver from matlab I never get it right
hdlDoc.CustomDocumentProperties.Add('Name','ExtraProp','LinkToContent',false,'Type',msoPropertyTypeString,'Value','NewValue');
I tried several variants:
false, 'False'
msoPropertyTypeString, 'msoPropertyTypeString', 'String', 'Text'
The invoke method gives me the following. And the ms office documentation calls for a 4 as string type https://learn.microsoft.com/de-de/office/vba/api/office.msodocproperties
Add = handle Add(handle, ustring, bool, int32, Variant(Optional))
But putting in int32(4) as Type does not work either.
Does someone knows what I am doing worng?
0 Comments
Answers (1)
dpb
on 7 Mar 2025
Edited: dpb
on 8 Mar 2025
Let's start with your code to create the actxserver and open the document to get the ActiveDocument object handle...
Then, when working with Word/Excel/... via ActiveX, you/MATLAB don't have the VBA compiler at hand to interpret the higher-level VBA syntax, specifically named parameters. Everything has to be passed by position using default or [] for any unused parameters in the argument list prior to any that are used.
I finally managed to get the search to point to the <DocumentProperties.Add method> where we see the syntax is
expression.Add (Name, LinkToContent, Type, Value, LinkSource)
expression Required. A variable that represents a DocumentProperties object. The custom DocumentProperties object.
So, pass the values by postion, leaving out the names since don't have VBA to translate source code using them to actual arguments...
hdlDoc.CustomDocumentProperties.Add('ExtraProp',false,msoPropertyTypeString,'NewValue');
You'll have to create a parameter or the msoPropertyTypeString variable to assign it a value in MATLAB context, too. If you envision doing this a lot, you may want to build some classes that hold the most commonly used constants for documentation purposes as well as to keep from having to look them up/redefine them locally all the time. As an example, a short one from the very small set I built of Excel enumerations out of the cast of thousands is XlDirection
>> type XlDirection.m
classdef XlDirection
properties (Constant)
xlDown = -4121; % Down.
xlToLeft = -4159; % To left.
xlToRight= -4161; % To right.
xlUp = -4162; % Up.
end
end
>>
7 Comments
dpb
on 13 Mar 2025
Here's <a link I found on causes for the "too many arguments" error> -- there are some other things that can produce the error doing with possible hidden arguments or control arrays; my guess is that something like that is going on with the properties collection but I don't have enough knowledge of Word to know what, specifically that might be. Again, I'm sure it's got to do with not having the VBA compiler available to do its magic behind the scenes transalation from the documented or auto-generated macro code in the MATLAB environment.
See Also
Categories
Find more on Use COM Objects in MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!