Free Support Forum - aspose.com

Cannot get CustomDocumentProperties or BuiltInDocumentProperties to create or update Word Properties

I cannot get the properties to be applied or updated. I have read the documentation and tried the basic examples supplied on the Aspose site without any success. I have tried a .docx and a .doc file to no avail but can loop through the existing properties successfully. I am getting no compilation errors and can apply other changes to the Word document such as appending multiple Word docs into one. There are no access rights issues and I have checked the Advanced Properties Dialog Box to ensure they were not hidden in the Custom properties tab.

What am I doing wrong?

Here is an extract of my testing code:

        Document doc = new Document("E:\\Properties.doc");

        CustomDocumentProperties props = doc.CustomDocumentProperties;

        Console.WriteLine("1. Document name: {0}", doc);

        Console.WriteLine("2. Built-in Properties");
        foreach (DocumentProperty prop in doc.BuiltInDocumentProperties)
            Console.WriteLine("{0} : {1}", prop.Name, prop.Value);

        Console.WriteLine("3. Custom Properties");
        foreach (DocumentProperty prop in doc.CustomDocumentProperties)
            Console.WriteLine("{0} : {1}", prop.Name, prop.Value);

        if (props["Authorized"] == null)
        {
            props.Add("Authorized", true);
            props.Add("Authorized By", "John Smith");
            doc.UpdateFields();
       }

@MaxAsposeTex,

Thanks for your inquiry. Please ZIP and upload a standalone simple console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please also share the Word document you are getting this problem with here for testing. We will then start investigation into your issue and provide you more information.

Do you need the Aspose.Words.dll in the zip because that will make it past the allotted file size for upload.

Cheers,
MT

@MaxAsposeTex,

No. Please remove Aspose.Words.dll file(s).
We just need piece of code (console app) and sample Word document you are getting this problem with.

Here you go. All assistance appreciated.

ConsoleApplication7.zip (259.3 KB)

@MaxAsposeTex,

But, can you please try running the following code:

Document doc = new Document(MyDir + @"Properties.doc");

// CustomDocumentProperties props = doc.CustomDocumentProperties;
CustomDocumentProperties docProperties = doc.CustomDocumentProperties;

Console.WriteLine("1. Document name: {0}", doc);

Console.WriteLine("2. Built-in Properties");
foreach (DocumentProperty prop in doc.BuiltInDocumentProperties)
    Console.WriteLine("{0} : {1}", prop.Name, prop.Value);

Console.WriteLine("3. Custom Properties");
foreach (DocumentProperty prop in doc.CustomDocumentProperties)
    Console.WriteLine("{0} : {1}", prop.Name, prop.Value);

// CustomDocumentProperties docProperties = doc.CustomDocumentProperties;

if (docProperties["Authorized"] == null)
{
    docProperties.Add("Authorized", true);
    docProperties.Add("Authorized By", "John Smith");
    docProperties.Add("Authorized Date", DateTime.Today);
    docProperties.Add("Authorized Revision", doc.BuiltInDocumentProperties.RevisionNumber);
    docProperties.Add("Authorized Amount", 123.45);
}

Console.WriteLine("---------AFTER CHANGES----------------");
foreach (DocumentProperty prop in doc.CustomDocumentProperties)
    Console.WriteLine("{0} : {1}", prop.Name, prop.Value);

doc.Save(MyDir + @"18.4.docx");

Also, please see 18.4.zip (6.1 KB) and try running the following code:

Document doc = new Document(MyDir + @"18.4.docx");
foreach (DocumentProperty prop in doc.CustomDocumentProperties)
    Console.WriteLine("{0} : {1}", prop.Name, prop.Value);

Your supplied code works for me. Apparently I forgot to save the document after adding the new properties. Derp.

Thank you.

@MaxAsposeTex,

Thanks for your feedback. Please let us know anytime you have any further queries.