MailMerge - Change format/Style/color - No values in FieldValue

Hi,
I have an evaluation version of Aspose.Words.
I am trying to replace a merge field with a plain text but at the same time applying a desired format/Style/Color.
‘till yet I haven’t succeed, could you please help me.

doc = new Document(sLogFilePath);
InsertText("PlaceHolder1", "Change me style and color");
public void InsertText(string placeHolder, string replaceText)
{
    doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeField);
    doc.MailMerge.Execute(new string[] { placeHolder }, new object[] { replaceText });
}
private void HandleMergeField(object sender, MergeFieldEventArgs e)
{
    DocumentBuilder builder = new DocumentBuilder(e.Document);
    if (e.FieldName.Equals("PlaceHolder1"))
    {
        builder.MoveToMergeField(e.FieldName);
        string textInputName = = (string)e.FieldValue; ***Is empty ???
        builder.InsertText... () // Insert my value of (replaceText)
        //***Change stylte and color.
    }
}

Hi
Thanks for your inquiry. I tried to use the following code and it works fine.

public void TestMailMerge()
{
    // Open Template document
    Document doc = new Document(@"306_110990_CmJoshua\in.doc");
    // Add mergeField evnt handler
    doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeField);
    // Execute mail merge
    doc.MailMerge.Execute(new string[] { "PlaceHolder1" }, new object[] { "Change me style and color" });
    // Save output document
    doc.Save(@"306_110990_CmJoshua\out.doc");
}
private void HandleMergeField(object sender, MergeFieldEventArgs e)
{
    DocumentBuilder builder = new DocumentBuilder(e.Document);
    if (e.FieldName.Equals("PlaceHolder1"))
    {
        // Move to mergefield
        builder.MoveToMergeField(e.FieldName);
        // Get value
        string textInputName = (string)e.FieldValue;
        // Change color
        builder.Font.Color = Color.Red;
        // Insert text
        builder.Write(textInputName);
    }
}

Hope this helps.
Best regards.

Hello,

Thank you for your reply, the snipet you post works fine when I Merge once. But if I execute more than once the second time the value for “FieldValue” is empty.

Please see below the snipet which reproduces the error:

- Create a doc template with two Merge Fields so:
«[%PlaceHolder1%]»
«[%PlaceHolder2%]»

-Execute my snipet

Thanks for taking care of this enquire.

Best regards,
Joshua

public void TestMailMerge()
{
    // Open the template document
    Document doc = new Document(System.IO.Path.Combine(DocPath, "AlternatingRowsDemo.doc"));
    doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeFieldTest);
    InsertText(doc, "[%PlaceHolder1%]", "Change me style and color Green");
    InsertText(doc, "[%PlaceHolder2%]", "Change me style and color Yellow");
    doc.Save(@"306_110990_CmJoshua\out.doc");
}
public void InsertText(Document doc, string placeHolder, string replaceText)
{
    doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeField);
    doc.MailMerge.Execute(new string[] { placeHolder }, new object[] { replaceText });
}
private void HandleMergeFieldTest(object sender, MergeFieldEventArgs e)
{
    DocumentBuilder builder = new DocumentBuilder(e.Document);
    if (e.FieldName.Equals("[%PlaceHolder1%]"))
    {
        // Move to mergefield
        builder.MoveToMergeField(e.FieldName);
        // Get value
        string textInputName = (string)e.FieldValue;
        // Change color
        builder.Font.Color = Color.Green;
        // Insert text
        builder.Write(textInputName);
    }
    if (e.FieldName.Equals("[%PlaceHolder2%]"))
    {
        // Move to mergefield
        builder.MoveToMergeField(e.FieldName);
        // Get value
        string textInputName = (string)e.FieldValue;
        // Change color
        builder.Font.Color = Color.Yellow;
        // Insert text
        builder.Write(textInputName);
    }
}

Hi
Thanks for additional information. You can try using the following code snippet.

private void HandleMergeFieldTest(object sender, MergeFieldEventArgs e)
{
    DocumentBuilder builder = new DocumentBuilder(e.Document);
    if (e.FieldValue != null)
    {
        // Get value
        string textInputName = (string)e.FieldValue;
        // Move to mergefield
        builder.MoveToMergeField(e.FieldName);
        switch (e.FieldName)
        {
            case "[%PlaceHolder1%]":
                // Change color
                builder.Font.Color = Color.Green;
                break;
            case "[%PlaceHolder2%]":
                // Change color
                builder.Font.Color = Color.Yellow;
                break;
        }
        // Insert text
        builder.Write(textInputName);
    }
}

But I think that if you try using this then you never get thiserror.

doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeFieldTest);
string[] names = { "[%PlaceHolder1%]", "[%PlaceHolder2%]" };
string[] values = { "Change me style and color Green", "Change me style and color Yellow" };
doc.MailMerge.Execute(names, values);

Hope this helps.
Best regards.

Hi Alexey,
I appreciate very much your support on this.
The problem for me to use the code you provided me, is that this limits me to execute “MailMerge.Execute” to a one time.
Then It would need to collect all placeholders and values of the whole document which are many in string[] and then finally executes the MailMerge. To do this for me is not an option, because I have hundreds placeholders on one page, spited in different UserControls and up on some conditions, some are replaced others don’t.
Therefore I illustrated in my previews snippet what I called the method “InsertText” more than one because in between would be some conditions or being called from for the same Doc from different UserControls.
Using “Doc.Range.Replace” instead if works for me with no problem, but the problem is that our documents are using MargeFields and not “PlainText” .
I hope I made myself clear.
Thanks again for your support.
Best regards
Joshua

Hi Alexey,
I appreciate very much your support on this.
The problem for me to use the code you provided me, is that this limits me to execute “MailMerge.Execute” to a one time.
Then It would need to collect all placeholders and values of the whole document which are many in string[] and then finally executes the MailMerge. To do this for me is not an option, because I have hundreds placeholders on one page, spited in different UserControls and up on some conditions, some are replaced others don’t.
Therefore I illustrated in my previews snippet what I called the method “InsertText” more than one because in between would be some conditions or being called from for the same Doc from different UserControls.
Using “Doc.Range.Replace” instead if works for me with no problem, but the problem is that our documents are using MargeFields and not “PlainText” .
I hope I made myself clear.
Thanks again for your support.
Best regards
Joshua

Hi

Have you tried to use HandleMergeFieldTest i provided instead yours? I think that this should solve your problem.

Best regards.