Add custom property to word via actxserver

14 views (last 30 days)
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?

Accepted Answer

Philipp
Philipp on 23 Apr 2025
I did not find a correct way to perform the ActiveX code.
hdlDoc.CustomDocumentProperties.Add('ExtraProp',false,4,'NewValue');
Nevertheless, now I use a workaround. I wrote a word macro template (.dotm) with VBA code. It includes the Add-method with input variables. Then in Matlab, I use the ActiveX to load and run this macro.
%% Initiallize Word Connection
hdlWord = actxserver('Word.Application');
hdlWord.Visible = true;
%% Loading Macro Template
templateFile = [scriptPath '\MyMacros.dotm'];
hdlWord.Application.Templates.LoadBuildingBlocks;
hdlWord.Documents.Add(templateFile);
%% File and Location
docName = 'PropTest.docx';
fullLocation = [docPath '\' docName];
hdlDoc = hdlWord.Documents.Open(fullLocation);
hdlCDP = hdlDoc.CustomDocumentProperties;
%% Adding CDP
macroName = 'AddCustomProperty';
propName = 'ExtraProp';
propType = int32(4); % 2=Boolean.3=Date.5=FloatingPoint.1=Integer.4=String.
propValue = 'Hank!';
% Call the macro (now loaded from the template)
hdlWord.Run(macroName, propName, propValue);
Works fine. Downside: I always need this external macro file in the background.
  2 Comments
dpb
dpb on 23 Apr 2025
Minor coding nit...
%% File and Location
docName = 'PropTest.docx';
fullLocation = fullfile(docPath,docName);
Just out of curiosity, how about attaching the macro code? It still should be possible to do it directly with the right template. I don't know enough about Word to start with and the custom properties stuff in particular that I understand them...the VBA doc shows something about "SmartTags" and the custom properties somehow seem attached to them. I never heard of them, but then the code above does something about loading some template which must have something like that in it, I'm guessing? Would probably need a sample document to play with that works this way to get anywhere, and even then it wouldn't be any guarantee, but "just maybe!".
I will note that in the code above, propType appears totally superfluous.
Philipp
Philipp on 25 Apr 2025
Hi dpb,
thanks for your suggestions. propType came in later. I rewrote the macro again to accept any type of property. But I forgot to clean the matlab code.
I am no programmer and I am good enough with that one solution I found. Although I like the idea of having one template, that already includes the macro code, this would not work. I am doing that for a community and I am afraid that not everybody would always go and get the latest version.
Thanks again for your suggestions and patience

Sign in to comment.

More Answers (1)

dpb
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
dpb on 13 Mar 2025
Good luck...let us know what the final answer is...
dpb
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.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!