Retrieve comment of a MergeField

Dear support team,
I’m using Aspose.words c#, and I have a word document that contains a mail merge field called <<mergeFieldTest1>> and also a comment associated with the field(i.e. “This field is used for test purpose.”). So How could I extract the respective comment of that particular merge field using Apose.words.

Regards,
Jobin

@WJobin Comments in MS Word documents cannot be associated with merge field or with some text. Comment is the same node as other and can be placed next to the merge field also comment can have range that wraps some content, in such case you can see commented content in MS Word document like this:


Internally such comment look like this:

Or in XML representation like this:

<w:p w14:paraId="5129C0EE" w14:textId="19271A4B" w:rsidR="002B2CA3" w:rsidRDefault="008B440B">
	<w:r>
		<w:t xml:space="preserve">This is </w:t>
	</w:r>
	<w:commentRangeStart w:id="0"/>
	<w:r>
		<w:t xml:space="preserve">some </w:t>
	</w:r>
	<w:commentRangeEnd w:id="0"/>
	<w:r>
		<w:rPr>
			<w:rStyle w:val="CommentReference"/>
		</w:rPr>
		<w:commentReference w:id="0"/>
	</w:r>
	<w:r>
		<w:t xml:space="preserve">text </w:t>
	</w:r>
</w:p>

So to find the required comment you should get the comment next to the merge field. You can attach your document here for further analysis. We will check it and provide you more information.

But I found a way to fetch this.

using Aspose.Words;
using Aspose.Words.Fields;
using Aspose.Words.Comments;
using System;

class Program
{
    static void Main()
    {
        // Load the document
        Document doc = new Document("YourDocument.docx");

        // Get all merge fields in the document
        foreach (Field field in doc.Range.Fields)
        {
            if (field.Type == FieldType.FieldMergeField)
            {
                FieldMergeField mergeField = (FieldMergeField)field;
                Console.WriteLine($"Merge Field: {mergeField.Name}");

                // Find the comment associated with this merge field
                Comment relatedComment = FindRelatedComment(mergeField);

                if (relatedComment != null)
                {
                    Console.WriteLine($"Related Comment: {relatedComment.GetText().Trim()}");
                }
                else
                {
                    Console.WriteLine("No comment found for this merge field.");
                }

                Console.WriteLine("--------------------------");
            }
        }
    }

    // Function to find the closest comment for a given merge field
    static Comment FindRelatedComment(FieldMergeField mergeField)
    {
        Node node = mergeField.Start.NextSibling;

        while (node != null)
        {
            if (node is Comment comment)
            {
                return comment;
            }
            node = node.NextSibling;
        }

        return null;
    }
}

@alexey.noskov Could you please check my code, and please revert if there is any problem like you said.

@WJobin Your code does exactly what was suggested, i.e. it searches for comment next to merge field. The code looks good to me.

1 Like

Thanks for your support

1 Like