List all custom properties and add a new one with actxserver Word
Afficher commentaires plus anciens
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
Plus de réponses (1)
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
- 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
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.
Philipp
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?
Philipp
le 17 Juil 2024
dpb
le 17 Juil 2024
"...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..
Philipp
le 14 Oct 2024
Philipp
le 14 Oct 2024
dpb
le 14 Oct 2024
" 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...
Catégories
En savoir plus sur Excel Add-Ins dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!