How would I filter a range?

I’m new to GemBox. I’m trying to restrict the contents of a range. The syntax I’d like to use in the Word document would be something like this:

{MERGEFIELD RangeStart:Assets \* AssetTypeId in (42, 1337)}

But in my event handler for FieldMerging when I inspect the FieldMergingEventArgs.MergeContext.RangeName, it only has the name up to the first space (i.e. “Assets”). I’ve inspected the rest of the context and I cannot see where the remainder of the merge field text has gone (the \* AssetTypeId in (42, 1337) text).

Is this the right way to filter? Is there another/better way to filter a range?

Hi Bernhard,

Please try again with these latest NuGet packages:

Install-Package GemBox.Document -Version 35.0.1474-hotfix

We added support for MailMergeContext.RangeStartField and MailMergeContext.RangeEndField properties.
So now, you can use something like the following:

document.MailMerge.FieldMerging += (sender, e) =>
{
    // ...

    string rangeInstruction = e.MergeContext.RangeStartField.GetInstructionText();

    // ...
};

I hope this helps.

Regards,
Mario

1 Like

Thank you so much for replying Mario.

Is there any documentation on how this would/could appear in the Word template file?

Hi Bernhard,

What exactly do you mean by that?
Note that GemBox.Document’s mail merge doesn’t have filtering capabilities.

This “* AssetTypeId in (42, 1337)” is your custom instruction text and I presumed you plan to handle that in the FieldMerging event to customize your mail merge process.
The rangeInstruction string will contain that instruction text which you’ll need to handle further according to your requirements.

Regards,
Mario

That’s exactly right. The requirement is to restrict the assets to those with a type identified 42 or 1337. I was planning on doing this in the MailMerge.FieldMerging handler.

This looks like the most appropriate place to “document” the filter in the Word template, but if there’s a better way please let me know.

I’m currently unable to test with the latest hotfix version due to a license issue:

The serial key is not valid. Please renew your license to continue using this version, or use an older version.

I’ll see if version 33 has the rangeInstruction you mentioned.

This looks like the most appropriate place to “document” the filter in the Word template, but if there’s a better way please let me know.

You are right, this is the most appropriate place to do this.

I’ll see if version 33 has the rangeInstruction you mentioned.

This latest version I sent you has MailMergeContext.RangeStartField and MailMergeContext.RangeEndField properties. The previous versions don’t have them so you won’t be able to get the rangeInstruction string.

1 Like

I’ll speak to our licensing department about getting a license for the latest version.

I apologise now; it may take some time before I am able to respond and mark this as the solution.

I’m not sure if I’ve done this correctly, but I could not find a reference to rangeInstructions. I’ve used GetInstructionText on the RangeStartField to get the instruction text, which I split at the \* and work work with the remainder. Hoping this might help others, or you can tell me there’s an easier way.

private void MailMergeOnFieldMerging(bool singleMergeVariablesOnly, FieldMergingEventArgs e)
{
    ...
    var rangeInstructionText = e.MergeContext.RangeStartField.GetInstructionText();
    ...
}

Yes Bernhard, that is what you need to do.

You retrieve the RangeStartField.GetInstructionText() string and then you need to further process it based on your requirements.

1 Like