ASK FIELDS and BOOKMARKS

I am running the following code as part of a solution to replace fillin and ask fields. It was originally gotten from this post Word Merge Fields during mail merge

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 for this?

kind regards,
linda

This message was posted using Page2Forum from Working with Bookmarks - Aspose.Words for .NET

Hello
Thank you for your interest in Aspose.Words. Could you please attach your input and expected output documents here for testing? I will check them and provide you more information.
Best regards,

Thank you for your help. Attached are 3 files.

text file containing code
actual word document output by code
expected results

linda

Hi Linda,
Thank you for additional information. You should simply surround the value inserted by the code with bookmark. Please see the following modified code:

private void ProcessDocument()
{
    // Open document
    Aspose.Words.Document doc = new Aspose.Words.Document(@"Test001\in.docx");
    // 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.FieldAsk)
        {
            // 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*ASK\\s+\"(?[^\"]+)\"\\s+\\\\d\\s+\"(?[^\"]+)\"");
            Regex regex = new Regex("\\s*ASK\\s+(?\\S+)\\s+\"(?[^\"]+)\"\\s+\\\\d\\s+\"(?[^\"]*)\"");
            Match match = regex.Match(fieldCode);
            // Change field code and field value
            if (match.Groups["question"].Value == "How are you?")
            {
                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);
            // We should surround the value with bookmark.
            string bookmarkName = match.Groups["bookmark"].Value;
            builder.StartBookmark(bookmarkName);
            builder.Write(fieldValue);
            builder.EndBookmark(bookmarkName);
        }
    }
    builder.Document.UpdateFields();
    // save output document
    doc.Save(@"test001\out.docx");
}

Hope this helps.
Best regards,

Alexey,
Your code works wonderfully in my test! The code in the existing system isn’t behaving as nicely - though it is close.
What’s happening… in the test above:
The following Word entry produces no result. Which is the desired behavior. (I’d like it to only output on references and if-then-else conditions. Not on the ask statement.)
{ASK ME “How are you?” \d “1” \o}
However the existing code outputs for this line whatever data the end user typed.
I don’t believe the problem is in the code you provided - I tested that. Without looking at the code (which would be too difficult to share) would you have any ideas where I should start to look for such a problem?
Thank you so much for all your help today. I hated to have spent so much time on this with no result!
kind regards,
linda

Alexey,
I was able to narrow down the problem to one of these two routines…

ConvertFieldsToStaticText(doc, FieldType.FieldIf);
UnlinkFields(doc);

I’ll see what I can find and report back if helfpul.
Thank you again.

Hi Linda,

Thanks for your inquiry.

You may be able to fix your issue quickly by using an updated version of the ConvertFieldsToStaticText method, just in case you are using an older version of the code.

You can find the most recent version in the documentation here: https://docs.aspose.com/words/net/replace-fields/

Thanks,

Alexey,

We recently upgraded to version 11 for Aspose for Word. Consequently, the Ask fields no longer work as defined here. Do you have any suggestions?

thanks for any help!

Retracting. :slight_smile: (Pardon.)