We're sorry Aspose doesn't work properply without JavaScript enabled.

# Add Update Custom Word Document Property Fields DOCPROPERTY DOCVARIABLE using C# .NET

Hello,
I just started using Aspose.Words. I read the guide and the documentation made sense to me, so I just started to try out some things… First thing to do for me is to populate custom docproperty fields in a word document template.
Some of the properties look like this: { DOCPROPERTY text *\MERGEFORMAT }. When I edit the value of this property, like this:

if (prop.Name == "text") {
prop.Value = "newtext"; }
doc.UpdateFields();


, it doesn’t work. It says: Error! Unknown document property name.
Docproperties that don’t have the *\MERGEFORMAT thing update just fine…
Then there’s also a DOCVARIABLE with a *\MERGEFORMAT. When I try to update it, like this:

doc.Variables["docvar"] = "New variable text!";


, it doesn’t give the error, but it doesn’t update the value either.
I noticed that when I remove the *\MERGEFORMAT from the docproperties and the docvariable, the update does work fine. But how to make my code handle these custom doc properties and the doc variable?
Then there’s another thing… When I use our business software to populate the more complex fields in the .dot and I save it as a .doc, which I then use with the Aspose application to populate the more simple fields, I get a runtime nullpointerexception in the UpdateFields method…:

[NullReferenceException: Object reference not set to an instance of an object.]
Aspose.Words.Fields.Field.set_Result(String value) +336
ಮ.ኣ.཯() +63
Aspose.Words.Range.UpdateFields() +147
Aspose.Words.Document.UpdateFields() +30


I am afraid I cannot send attach the documents I am using, because they are confidential, but maybe you have some ideas about what’s wrong with what and can push me in the right direction?

Hi
Thanks for your inquiry. Most probably property and variable with such names doesn’t exist in the document. Please try using the following code:

Document doc = new Document(@"Test127\in.doc");
// Update value of custom document property if property exists
if (doc.CustomDocumentProperties.Contains("text"))
doc.CustomDocumentProperties["text"].Value = "this is new value";
else
// Update value of document variable if variable exists
if (doc.Variables.Contains("docvar"))
doc.Variables["docvar"] = "this is new value of variable";
else
doc.Variables.Add("docvar", "This is new value of variable");
doc.UpdateFields();
doc.Save(@"Test127\out.doc");


You can also simplify your document (remove all confidential data) and attach it here for testing.
Best regards.

It doesn’t seem to be the problem though. I still get the same error. Also when I pasrse through all props and I ask their name, value and type, it all goes well. The naming is not the problem.
I have attached the .dot so you can test with it for a bit. Hope you can help me.
I will continue working on this next friday, so then I’ll let you know if you helped.

Hi
Thank you for additional information. I managed to reproduce the problem and created new issue #6094 in our defect database. I will notify you as soon as it is fixed.
Best regards.

I work in the same company as Jip, the first poster, and I think I have found the problem. Word uses both “*\Mergeformat” and “Mergeformat" for mailmerge. Aspose only gives errors when "\Mergeformat” is used. If, in the DOT for instance, the *\ is replaced by a *, Aspose doesn’t give any problems.

Maybe both variants could be implemented in Aspose? As far as I can see Word doesn’t handle both cases differently.

With regards,
Peter Wagenaar

Hi
Thanks for your inquiry. “* MERGEFORMAT” is correct one. Did you get incorrect document from your client or is it your own document created for testing? When I try to create such document using MS Word, Word adds “MERGEFORMAT” to name of mergefield. So I don’t think the “*” should be supported.