Redefining SET's value

I am wanting to find a way to use SET like an ordinary variable from within the Document template.

{SET x 0}
{REF x}

{IF this = that "{SET x 1}"}
{IF dog = friendly "{SET x 2}"}

The {REF x} should show the most recent value of x. Word does not work this way, and the competing values do not process as expected. If you update fields, it seems the first SET trumps. To get the most recent SET to trump, you’d have to go find it and then update only that one. Then, you’d have to find all the {REF x} fields and update them, too. While this can be done, it seems like a dirty way to do it. Plus, subsequently calling Document.Fields.Update() will change the result back to the first SET x.

Are there any clean, out-of-box ways to achieve this?

Per Aspose documentation: Badly formed bookmarks or bookmarks with duplicate names will be ignored when the document is saved.

This is unfortunate. I am stuck.

Hi Jeff,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

create a standalone/runnable simple application (for example a Console
Application Project
) that demonstrates the code (Aspose.Words code) you used to generate
your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will investigate as to how you are expecting your final document be generated like.

Unfortunately, it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to reproduce the problem. As soon as you get these pieces of information to us we’ll start our investigation into your issue.

Thanks for responding, Tahir. I am still thinking on how to do what I want.

In essence, I am trying to create a variable in a Word template and change its value from within the template as often as I need to. SET allows you to set a value, but only once. You cannot change it, because that would mean you are creating a duplicate SET (i.e., a duplicate bookmark, which is forbidden).

I don’t consider this a bug, but I do consider it a feature I’d sorely love to have. There are many reasons why a person might want to create a variable in a template and then, change its value throughout the template - such as adding the total value of line items in an invoice, for example. (Total += itemPrice * Quantity). That’s just one of many reasons I can think of.

Hi Jeff,

Thanks for your inquiry. It is by MS Word design that a Word document must have unique names for bookmarks. You can not insert two bookmarks with the same names.

As per my understanding, you want to use the Set and Ref fields in your document and use some variable values set by Set field. Note that set fields establish the contents of each of bookmarks inside the document, and Ref fields used to display the contents of the bookmarks. I have created a sample document which shows the behaviour of these fields. Please check the attached document.

Could you please elaborate your query along with input and expected output documents here for our reference? Perhaps, there is some other way to achieve your requirements. We will then provide you more information on this along with code.

Ok, Tahir. I have attached a docx file for your information. If you have any questions, please let me know. Thanks.

Hi Jeff,

Thanks for sharing the detail. Please use the following code example to achieve your requirements. This code example does the followings:

  1. Get the last Set field result
  2. Insert bookmark at the place of first Set field
  3. Remove all Set fields from the document.
  4. Insert new Set field at the position of inserted bookmark with Set field result get in step 1.

Hope this helps you. Please let us know if you have any more queries.

var doc = new Document(MyDir + "Tahir+Sample.docx");
var builder = new DocumentBuilder(doc);
String fieldResult = "";
ArrayList fields = new ArrayList();
// Get the result of last Set field
foreach (Field field in doc.Range.Fields)
    if (field.Type == FieldType.FieldSet)
        if (field.Result.ToString().Trim() != "")
            fieldResult = field.Result.ToString().Trim();
            Console.WriteLine("Field Result" + fieldResult);
// Move to first Set field
builder.MoveToField((Field)fields[0], true);
// Remove all Set fields
for (int i = 0; i < fields.Count; i++)
// Set the title
if (fieldResult != "REMOVEPARA")
    Field setfield = builder.InsertField(@"Set MyTitle ""The History of the World""");
else // Remove title
    Field setfield = builder.InsertField(@"Set MyTitle");
doc.Save(MyDir + "Out.docx");