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

Free Support Forum - aspose.com

Merge ASK field value

Hi, we have a document which contains ASK and Fill-in fields. I figure out how to convert Fill-in fields to merged fields to work during mail-merge process but I cannot figure out how it can work with ASK field.
I have the answers for ASK fields, and I want to replace during the merge process.
Should I convert the ASK field to bookmark? I appreciate any recommendation here

@minaeimi You can use code like the following to work with FieldAsk:

Document doc = new Document(@"C:\Temp\in.docx");
            
// Loop through the fields in the document.
foreach (Field field in doc.Range.Fields)
{
    if (field.Start.FieldType == FieldType.FieldAsk)
    {
        FieldAsk ask = (FieldAsk)field;
                    
        // Get the question.
        Console.WriteLine(ask.PromptText);

        // Set the value.
        doc.Range.Bookmarks[ask.BookmarkName].Text = "Hello, world!!!";
    }
}

doc.Save(@"C:\Temp\out.docx");

Hi Alexey,

Thanks for your reply.
Strangely docOrg.getRange().getBookmarks() doesn’t return back all the bookmarks.
Is there any reason?

@minaeimi Most likely, you are using Aspose.Words in evaluation mode and the document is truncated due to the evaluation version limitation. You can request a temporary license to test Aspose.Words without evaluation version restrictions.

To make it more clear, I have following code in the word file.
{ASK BASS “If the company has employees enter ‘1’ otherwise leave blank”}{IF {REF BASS *MERGEFORMAT}= “1” “the company has employees.” “”}

In our application we know the answer for “If the company has employees enter ‘1’ otherwise leave blank”, and during mail marge based on that answer which is either 1 or blank the correct text should be displayed.

I tried your code, but docOrg.getRange().getBookmarks() function doesn’t return BASS as bookmark.
Any help is appreciated.

thanks for your reply. No we have product family license, we bought it few 2 days ago.

@minaeimi Please attach your document here for testing. We will check the issue and provide you more information.

@alexey.noskov how I can send it to you privately? I am not allowed to share it on public forum.

@minaeimi It is safe to attach documents in the forum. Your attachments can be downloaded only by you and Aspose staff.

Mina_test1.docx (57.4 KB)
@alexey.noskov I attached the word file.

@minaeimi Thank you for additional information. In your document there is an ASK field without bookmark. Here is a part of XML of your document:

<w:r w:rsidRPr="00E3367D">
	<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r w:rsidRPr="00E3367D">
	<w:instrText xml:space="preserve"> ASK  BOB "If money laundering checks are done electronically, enter '1' otherwise, if you want copies of passports etc, leave blank"</w:instrText>
</w:r>
<w:r w:rsidRPr="00E3367D">
	<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r w:rsidRPr="00E3367D">
	<w:t>a.m.</w:t>
</w:r>
<w:r w:rsidRPr="00E3367D">
	<w:fldChar w:fldCharType="end"/>
</w:r>

As you can see field value of the ASK field does not have a bookmark. If you update fields in the document using MS Word, it automatically creates a bookmark and in the XML you will see the following:

<w:r w:rsidRPr="00E3367D">
	<w:fldChar w:fldCharType="begin"/>
</w:r>
<w:r w:rsidRPr="00E3367D">
	<w:instrText xml:space="preserve"> ASK  BOB "If money laundering checks are done electronically, enter '1' otherwise, if you want copies of passports etc, leave blank"</w:instrText>
</w:r>
<w:r w:rsidRPr="00E3367D">
	<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:bookmarkStart w:id="0" w:name="BOB"/>
<w:r w:rsidR="00263C94">
	<w:t>a.m.test</w:t>
</w:r>
<w:bookmarkEnd w:id="0"/>
<w:r w:rsidRPr="00E3367D">
	<w:fldChar w:fldCharType="end"/>
</w:r>

As you can see bookmark BOB was created.
You can achieve the same using Aspose.Words. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Loop through the fields in the document.
foreach (Field field in doc.Range.Fields)
{
    if (field.Start.FieldType == FieldType.FieldAsk)
    {
        FieldAsk ask = (FieldAsk)field;

        if (doc.Range.Bookmarks[ask.BookmarkName] == null)
        {
            ask.Result = "";
            builder.MoveTo(ask.End);
            builder.StartBookmark(ask.BookmarkName);
            builder.EndBookmark(ask.BookmarkName);
        }

        // Set the value.
        doc.Range.Bookmarks[ask.BookmarkName].Text = "Hello, world!!!";
    }
}

doc.Save(@"C:\Temp\out.docx");

Many thanks @alexey.noskov it works fine.