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

10 ビュー (過去 30 日間)
Philipp
Philipp 2024 年 7 月 12 日
回答済み: Philipp 2025 年 4 月 25 日
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?

採用された回答

Philipp
Philipp 2025 年 4 月 25 日
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.

その他の回答 (1 件)

dpb
dpb 2024 年 7 月 12 日
編集済み: dpb 2024 年 7 月 12 日
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 件のコメント
Philipp
Philipp 2024 年 10 月 14 日
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?
dpb
dpb 2024 年 10 月 14 日
" 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...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Export to MATLAB についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by