Question

Trying to access CustomDocumentProperty from VBA code . I check the custom.xml file the custom property exists . Anything im missing ? here is the docm file !

Sample document attached

Updated

I tried to add using the below code but it does not add anything . I need to mark a file as processed . Is there any other way i can accomplish that?

With ActiveDocument.CustomDocumentProperties
    .Add Name:="CustomNumber", _
        LinkToContent:=False, _
        Type:=msoPropertyTypeNumber, _
        Value:=1000
    .Add Name:="CustomString", _
        LinkToContent:=False, _
        Type:=msoPropertyTypeString, _
        Value:="This is a custom property."
    .Add Name:="CustomDate", _
        LinkToContent:=False, _
        Type:=msoPropertyTypeDate, _
        Value:=Date
End With

Updated link to add custom field document

Was it helpful?

Solution

In the XML (custom.xml), your Custom Document Property has fmtid="{3DC82474-74E0-4A77-944F-F36D8A5B0E58}" (or something like that).

It needs to be {D5CDD505-2E9C-101B-9397-08002B2CF9AE}, or Word will not recognise it as a Custom Document Property.

For your updated question, there's nothing wrong with your VBA code, but there is an error in Word. hen you insert custom document properties, Word does not mark the document as "Dirty", so if you have not changed the document in any other way, when it closes, it does not save the changes to the properties, unless you manually go into the Custom Document Properties dialog. But you can't do that when the document_close event is running.

To deal with that I suggest you put

ActiveDocument.Saved = False

at the end of your Document_Close event handler. BTW, at the moment your code will cause an error if the properties already exist.

I don't believe this is mentioned in the ISO standard or even in Microsoft's Implementation Information document ([MS-OI29500].pdf - there is some info. in the latter but it is not helpful and actually a bit confusing.

Some of these standard IDs are listed here but I can't find anything about the one that starts with "{3D". I would guess that someone has created the document using Office Open XML directly and has just inserted "any old GUID".

Personally, I'd go into the XML and fix the GUID. (Update - I tried that, but failed every time. Best to start with a new doc. and insert the property via the standard Custom Document Properties dialog, I think).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top