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

Free Support Forum - aspose.com

Insert a Word Quick Part

We have a requirement to insert quick parts into Word documents. We need to insert a field from the document’s CustomDocumentProperties collection. Can we do that using Aspose?


I saw that DocumentBuilder.InsertField() method is used to insert quick parts. However, the method throws an exception if I provide the name of a field that is in the document’s CustomDocumentProperties collection.

FYI, the custom document properties come from SharePoint. The document is saved in a SharePoint library that adds and sets those fields via the document’s content type.

Here is an example:

try
{
Document doc = new Document(@“somepath\somedoc.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);

string fieldName = null;

if (doc.CustomDocumentProperties.Count > 0)
{
if (doc.CustomDocumentProperties[“FIELD_NAME”] != null)
{
fieldName = “FIELD_NAME”;
}
}

if (!string.IsNullOrEmpty(fieldName))
{
builder.MoveToSection(0);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Custom Property ");
builder.InsertField(fieldName);
doc.Save(doc.OriginalFileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

Hi,


Thanks for your inquiry. Please note that the DocumentBuilder.InsertField method can only be used to insert a “Word field” (such as PAGE, MERGEFIELD, NUMPAGES etc) into a document and updates the field result. However, you can use the following line of code to insert a custom field value in document:

builder.InsertField(" DOCPROPERTY propname ");

I hope, this helps.

Best regards,

Thanks for the response Awais. The suggestion works. However, if the custom property field is a SharePoint Managed Metadata field, it inserts the value in the following format:


52;#FIELD_VALUE|48414bfb-6543-43c8-b915-86dd51ee3e01

This is because the value of a standard MMD field in SharePoint will be in the format specified above. The expectation is to only insert the token “FIELD_VALUE”. Is this possible with Aspose? Do we have methods to handle this?

<o:p></o:p>

Hi,


Thanks for your inquiry. Yes, you can still achieve this by iterating through the Document.CustomDocumentProperties collection, then save the value of target field in string variable, after that extract the FIELD_VALUE token using standard string methods such as string.Split and finally insert the text using DocumentBuilder.Write() method. I hope, this helps.

Best regards,

Thanks for the response. I understand that this method writes the value to the document.


The requirement is to insert a quick part, which “displays” the value stored in the custom property of the document. It must change when the value of the custom property changes. Although the InsertField method does that, it doesn’t handle the managed metadata fields as expected.

So I am not sure if the suggested solution will work. It might “write” a value to the document, but I am not sure if it’ll reflect any changes made to the property.

Hi,


Thanks for your inquiry. We’re in communication with our development team and will get back to you soon.

Best regards,

Hi,


Thanks for being patient. I am afraid, the DOCPROPERTY field does not provide any specific switches to display only a part of the property value. So, basically we cannot do what MS Word cannot, and it always mimics the behavior of MS Word. I think you could update the document using Aspose.Words (if possible) to strip the token and replace values of the properties.

We also don’t think that we have any other option. When you say, “Although the InsertField method does that, it doesn’t handle the managed metadata fields as expected”, it does not seem correct because it is MS Word’s DOCPROPERTY field which is unable to handle metadata from SharePoint as expected; and Aspose.Words just repeats MS Word’s behavior. If we can help you with anything else, please feel free to ask.

Best regards,
Thank you for your response, however, I think we have a misunderstanding of the facts.

I've added several attachments which illustrate configuring Quick Parts in MS Word that target custom properties. Word displays only the caption part of the managed metadata field value (and is dynamic as the SharePoint field value changes)

We've tested this approach and it works. The only problem is that we have 200 documents to go through and a manual process is burdensome.

FYI, we do have a support agreement with Aspose if this request requires additional Aspose resources.

Thanks!

Hi,


Thanks for the additional information. When doing the steps mentioned in your screenshot, MS Word 2013 inserts a SDT (StructuredDocumentTag) in document which are also supported in Aspose.Words. Please see StructuredDocumentTag Class in Aspose.Words. We are also in communication with our development team to investigate this requirement and will get back to you soon.

Best regards,