Word Merge Fields during mail merge

I have a Word document template (.doc) with custom merge fields and Word fields like FILLIN.
I want to know whether I can archive the following using Aspose.

  1. Can I get the word merge fields using any property of Document or DocumentBuilder?
    Like for example, how do I know whether the document has got an ASK or FILLIN word field.

  2. How do I make the word merge fields to run?
    When I call : document.MailMerge.Execute(), it takes care of IF…ELSE fields, but what about the fields like ASK, FILLIN.
    I believe I need to open the document and present it to the user to get the inputs for those fields.

But when I open the merged document, it doesn’t open the FILLIN fields.
Please advice.

From one of the old posts, I can see that the Aspose Words doesn’t handle the FILLIN fields.
Aspose.Word Questions
Any changes have been made to help processing the FILLIN/ASK fields?

Does Aspose.Words provide a way to retrieve these fields, so that I can manually prompt the user for these field values?

Hi

Thanks for your inquiry. Could you please attach your template for testing. I will investigate the issue and provide you more information.
Regarding merge fields, you can use MailMerge.GetFieldNames() to get names of margefields in your document.
Best regards.

Please find a copy of the template I am trying with.

It is a simple template, just has got a FILLIN field like this.
{FILLIN "What is your name? \d " "Sudheer"}

If I get the merge fields using MailMerge.GetFieldNames(), I wont find FILLIN Field.
I believe Aspose is ignoring those fields.
But does Aspose Word expose any property to retrieve the Word merge fields like FILLIN, ASK etc?

Thanks.

Hi

Thanks for your inquiry. You cannot find your FILLIN field because it is not a merge field. You can find such fields using the following code:

//Open document
Document doc = new Document(@"Test091\in.doc");
//Create DocuentBuilder. It will help us to modify fields
DocumentBuilder builder = new DocumentBuilder(doc);
//Get collection of FieldStart nodes
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
//loop through all field starts and search for FILLIN fields
foreach (FieldStart start in starts)
{
    if (start.FieldType == FieldType.FieldFillIn)
    {
        //Every field in MS Word document consists of FieldStart, FieldSeparator, FieldEnd nodes
        //And Runs that represent field code and field value (displayed text)
        //If you need to change value of fillin field, you should change field code
        //And field value
        Node fieldSeparator;
        Node fieldEnd;
        //We should get field code and field value 
        string fieldCode = string.Empty;
        string fieldValue = string.Empty;
        Node currentNode = start.NextSibling;
        //Get Field code
        while (currentNode.NodeType != NodeType.FieldSeparator)
        {
            if (currentNode.NodeType == NodeType.Run)
                fieldCode += (currentNode as Run).Text;
            currentNode = currentNode.NextSibling;
            currentNode.PreviousSibling.Remove();
        }
        fieldSeparator = currentNode;
        currentNode = currentNode.NextSibling;
        //Get field value
        while (currentNode.NodeType != NodeType.FieldEnd)
        {
            if (currentNode.NodeType == NodeType.Run)
                fieldValue += (currentNode as Run).Text;
            currentNode = currentNode.NextSibling;
            currentNode.PreviousSibling.Remove();
        }
        fieldEnd = currentNode;
        //We should get question and answer from fillin field code
        Regex regex = new Regex("\\s*FILLIN\\s+\"(?[^\"]+)\"\\s+\\\\d\\s+\"(?[^\"]+)\"");
        Match match = regex.Match(fieldCode);
        //Change field code and field value
        if (match.Groups["question"].Value == "What is your name?")
        {
            fieldCode = fieldCode.Replace(match.Groups["answer"].Value, "Alexey");
            fieldValue = "Alexey";
        }
        //Insert new field code and field value
        builder.MoveTo(fieldSeparator);
        builder.Write(fieldCode);
        builder.MoveTo(fieldEnd);
        builder.Write(fieldValue);
    }
}
//save output document
doc.Save(@"Test091\out.doc");

Hope this could help you.
Best regards.

Cool…!
Thanks for the detailed reply.

Do you have an equivalent RegEx match string and logic for replacing ASK statements and their associated REF’s? If you could post it in VB, that’d be extra helpful but I think I can convert it if not.

Hi
Thanks for your request. Regex for parsing ASK fields is the same as for parsing FILLIN fields. The only difference is name of the fields:

Regex regex = new Regex("\\s*FILLIN\\s+\"(?[^\"]+)\"\\s+\\\\d\\s+\"(?[^\"]+)\"");
Regex regex = new Regex("\\s*ASK\\s+\"(?[^\"]+)\"\\s+\\\\d\\s+\"(?[^\"]+)\"");

Also, the difference between these fields is that FILLIN displays the answer, and ASK stores the answer in the hidden bookmark and then you refer this value using REF field.
So the technique will be the same, but you should also preserve bookmark in the field value and put the answer between BookmarkStart and BookmarkEnd nodes. Please see the attached screenshot.
Hope this helps.
Best regards.

I am running the following code as part of a solution to replace fillin and ask fields. It was originally taken from this post.

What I’ve found is that this section of code deletes document bookmarks:

//Get Field code 
while (currentNode.NodeType != NodeType.FieldSeparator)
{
    if (currentNode.NodeType == NodeType.Run)
        fieldCode += (currentNode as Run).Text;
    currentNode = currentNode.NextSibling;
    currentNode.PreviousSibling.Remove();
}
fieldSeparator = currentNode;
currentNode = currentNode.NextSibling;
//Get field value
while (currentNode.NodeType != NodeType.FieldEnd)
{
    if (currentNode.NodeType == NodeType.Run)
        fieldValue += (currentNode as Run).Text;
    currentNode = currentNode.NextSibling;
    currentNode.PreviousSibling.Remove();
}
fieldEnd = currentNode;

Which I’m suspicious is the reason my ASK bookmarks are not properly populated.

I am trying to finally achieve the following:

{ASK NUM1 "Select a number?" \d "3" \o}

You entered {NUM1}

{IF NUM1 = 3 "Default Value" "Not the default"}

I am new to ASPOSE and any advice is appreciated. Do you have any sample code that does this fully?

kind regards,
linda

Hi Linda,
Thanks for your request. Could you please attach your sample template here for testing and the expected output? We will check your documents and try to help you.
Best regards,