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.
@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:
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.
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;
}
}
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.