List all custom properties and add a new one with actxserver Word

Hi all,
I am using actxserver for Word-Documents and manipulating their custom document properties.
I know how to get and set these values as long as the properties are allready implemented.
But, how do I get a list of all custom document properties?
How do I add a new one?

 Réponse acceptée

Hi,
the solution I found on "How to list all custom document properties?"
hdlWord = actxserver('Word.Application');
hdlWord.Visible = true;
%% File and Location
docPath = 'folder';
docName = 'PropTest.docx';
fullLocation = [docPath '\' docName];
hdlDoc = hdlWord.Documents.Open(fullLocation);
hdlCDP = hdlDoc.CustomDocumentProperties;
%% Listing all CDPs
nCDP = hdlCDP.Count; % number of custom document properties
for i=1:nCDP
hdlProp(i) = hdlCDP.get('Item', i); % fill cdp handle vector with the interfaces
% For verification only
propName = hdlProp(i).get('Name');
disp(propName); % writing all CDP names
propValue = hdlProp(i).get('Value');
disp(propValue); % writing all CDP values
end
I know that this method of changing size of hdlProp(i) is not a good style, but I did not found a way to initialze an interface vector.

Plus de réponses (1)

dpb
dpb le 12 Juil 2024
Modifié(e) : dpb le 12 Juil 2024
That is a Word Q?, not MATLAB. See the <Word VBA Reference>. Look at the object model section and find the custom properties object. There's bound to be an Add method. Your task will be to convert the VBA syntax into a form ActiveX commands can execute. The problem is that the VBA compiler is not available to MATLAB so many of the high level features such as named parameters and multiple dot references to a method/property from an object handle aren't available, All parameters must be passed by position and all methods/properties have to be referenced only from the direct parent object. That's the minimum of the modifications that may be needed from VBA example code.
But, if there's a way with VBA, then with perserverence, one can manage to get it to work with ActiveX interface. It may not be trivial, but is possible.
ADDENDUM
  1. One "trick" is to record a macro that does what you want to do and then look at the VBA code generated and translate it as above for ActiveX.

9 commentaires

Philipp
Philipp le 15 Juil 2024
Modifié(e) : Philipp le 15 Juil 2024
Hi dpb, thanks for the quick answer. Sadly, I do not fully understand. Does this mean there is no command for adding new custom properties in a word document?
Until now I was using code like:
Word = actxserver('word.application');
WordObj = Word.Documents.Open(fullfile(pwd,'test.docx'));
CDP = WordObj.CustomDocumentProperties;
Then I got a handle for the custom properties. I thought maybe there was a way to use this handle to add a new property.
EDIT:
I found this article https://learn.microsoft.com/en-us/office/vba/api/word.customproperties.add as a reference. There is a way to add a new custom property to Word using VBA. Can someone help to translate into Matlab Code?
dpb
dpb le 15 Juil 2024
Modifié(e) : dpb le 15 Juil 2024
Look at <the rest of the doc for the object>, there's sample code to get you started. Or, use my previous suggestion to record a macro that does exactly what you want to do by using the user interface and then translate that.
It takes poking at the returned objects until you get the right object to use...when you've got it, you'll actually be able to see the .Add method with the debugger.
Just be prepared to kill a Word process and start over again while learning the ropes.
I don't even really know what a custom property is in Word; I have done a fair amount in Excel, but I "know nuthink!" about the Word object model.
ADDENDUM
Example code from above link includes
Sub AddProps()
With ActiveDocument.SmartTags(1)
.Properties.Add Name:="President", Value:=True
MsgBox "The XML code is " & .XML
End With
End Sub
The above illustrates what I pointed out before -- MATLAB doesn't know VBA so it can't use the With ... End With block syntax with the implied object returned by the reference to ActiveDocument.SmartTags(1). Instead, you'll have to save a temporary variable to hold the object then reference to it.
tag=ActiveDocument.SmartTags(1);
tag.Properties.Add("President",true)
It is possible the above will work, but note the conversion to MATLAB calling syntax dropping the names of the parameters and making them positional. I left the MATLAB string type; occasionally I seem to recall Excel having issues with it and generally use cellstr in ActiveX routines.
If it doesn't work in the above form, in the debugger make sure that tag.Properties will show the Add method; if it does, the issue will be something in the parameters, if the method isn't visible, then the other comment about the intermediary object is apropos, you'll need to then
tag=ActiveDocument.SmartTags(1);
prop=tag.Properties;
prop.Add("President",true)
Again as noted, you just have to play with it in the debugger until get the exact syntax and parameters needed...if you use the debugger and stop after you create the handle to the document, then you can poke at will and likely not crash Word; if you try to run trial code and it errors (almost inevitable at first), then you will almost certainly crash and burn and leave an activeX object in memory and the document open so you can't get at it again without physically killing the Word process from task manager.
Hi dpb,
thank you very much for your help. By no means I am a Software-Guy, I am not able to write VBA that quickly.
I tried several iterations of the code snippets above. Did not get it to work.
.SmartTags is not recognized by Matlab as well as .Properties. For properties I tried .CustomDocumentProperties and .builtInDocumentProperties which gives handles to the Word-Properties.
If the property exists, this works well:
hdlProp = get(hdlDoc.CustomDocumentProperties, 'Item', 'Property01');
get(hdlProp,'Value');
set(hdlProp,'Value','xyz');
Therefore I tried the following. But with using .Add, I cannot find the right syntax.
hdlDoc.CustomDocumentProperties.Add('President',true);
hdlDoc.CustomDocumentProperties.Add('President','true');
hdlDoc.CustomDocumentProperties.Add("President",true);
hdlDoc.CustomDocumentProperties.Add('Name','President',true);
hdlDoc.CustomDocumentProperties.Add('Name','President','Value',true);
hdlDoc.CustomDocumentProperties.Add('Name','President','Value','true');
hdlDoc.CustomDocumentProperties.Add('Item','President',true);
hdlDoc.CustomDocumentProperties.Add('Item','President','Value',true);
hdlDoc.CustomDocumentProperties.Add('Item','President','Value','true');
hdlDoc.CustomDocumentProperties.Add('President','Value',true);
hdlDoc.CustomDocumentProperties.Add('President','Value','true');
It always gives me the error message: Check for missing argument or incorrect argument data type in call to function 'Add'.
The .Add function seems to be in there, but I do not get it right.
dpb
dpb le 16 Juil 2024
Modifié(e) : dpb le 16 Juil 2024
<Smarttags> are something else again, it appears. The custom properties here are those for a smart tag and the .Add method is for another property for a smart tag it appears; I didn't catch that distinction in the quick look for the object.
There appears to be <another document-wide custom property>, but it is added by using the file menu and advanced properties for the document; this .Add method has nothing to do with those.
Which is it you're actually after, I'm guessing it is this type, not actually a smart tag?
Look at <Document Properties> instead, it's probably what you actually are looking for...
Hi dpb, thank you for all the help and ideas. Sadly, I am not able to program a macro using VBA. From profession, I am a hardware developer and doing matlab stuff, just because thats the only programming language I needed at the moment and it has the easiest access (development environment).
I will research the last recommendation. After that, I will just adapt my word templates as required and ignore the hard coded issues I face.
"...I am not able to program a macro using VBA"
You don't write the code, you just go through the steps and Word records them in the macro for you...then you go look at the auto-generated code to see what the VBA looks like to do what it was you wanted...
See <Create Macro>. To open VBA, you will need to add the "Developer" addin to get the ribbon access, but that is just adding the button to the ribbon if it hasn't been already..
Hi, I got recently back to this. I tried to record a vba macro for adding a new property. Sadly, the resulting macro is empty. It seems that the following click-chainis not recorded
File->Info->Properties->AdvancedProperties->Custom-> Name:New, Value:X -> Add->OK
Nevertheless, I copied this makro from another forum. It works perfectly in Word:
ActiveDocument.CustomDocumentProperties.Add _
Name:="NewProp", _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="NewValue"
The matlab code is not working. msoPropertyTypeString is unknown. I tried different things, nothing worked yet
hdlDoc.CustomDocumentProperties.Add('Name','NewProp','LinkToContent',false,'Type',msoPropertyTypeString,'Value','NewValue');
Someone has an idea here?
" msoPropertyTypeString is unknown..."
msoPropertyTypeString is a builtin constant of the mso; you'll have to look it up and pass the value or define needed constants somehow. I've begun building myself a class of Excel constants by the group and name for the purpose, but done nothing about Word...I've never found a really convenient listing for automagically building from other than the VBA documentation...

Connectez-vous pour commenter.

Catégories

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by