Replacing multiple strings in a .oft Outlook email template not working as expected

Viewed 283

I'm trying to replace multiple values within an Outlook Email Template file (.oft).

I did have this working at some point between commits, not entirely sure what's causing my issues.

As I understand HTMLBody is required to retain formatting.

The issue is that only the last two instances of .Replace are functioning

void BtnGenerateClick(object sender, EventArgs e)
{
    Outlook.Application emailApp = new Outlook.Application();
    Outlook.MailItem createTemplate = emailApp.CreateItemFromTemplate("C:\\Users\\USERNAME\\Documents\\OFTFiles\\test-template.oft") as Outlook.MailItem;
    createTemplate.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
    createTemplate.Subject = "Test Email sent " + '\u0040' + " " + tbTime.Text + " on " + currentDate;
            
    /* START Replace Text from Numeric Up Down Value Property  */
    var changed0 = createTemplate.HTMLBody.Replace("{0}",nud0.Value.ToString());
    createTemplate.HTMLBody = changed0;
            
    var changed1 = createTemplate.HTMLBody.Replace("{1}",nud1.Value.ToString());
    createTemplate.HTMLBody = changed1;
            
    var changed2 = createTemplate.HTMLBody.Replace("{2}",nud2.Value.ToString());
    createTemplate.HTMLBody = changed2;
            
    var changed3 = createTemplate.HTMLBody.Replace("{3}",nud3.Value.ToString());
    createTemplate.HTMLBody = changed3;
            
    var changed4 = createTemplate.HTMLBody.Replace("{4}",nud4.Value.ToString());
    createTemplate.HTMLBody = changed4;
            
    var changed5 = createTemplate.HTMLBody.Replace("{5}",nud5.Value.ToString());
    createTemplate.HTMLBody = changed5;
            
    var changed6 = createTemplate.HTMLBody.Replace("{6}",nud6.Value.ToString());
    createTemplate.HTMLBody = changed6;
    /* END Replace Text from Numeric Up Down Value Property */

    /* START Replace Text from Textbox Text Property */
    var changed7 = createTemplate.HTMLBody.Replace("{7}",tb7.Text);
    createTemplate.HTMLBody = changed7;
            
    var changed8 = createTemplate.HTMLBody.Replace("{8}",tb8.Text);
    createTemplate.HTMLBody = changed8;
            
    var changed9 = createTemplate.HTMLBody.Replace("{9}",tb9.Text);
    createTemplate.HTMLBody = changed9;
            
    var changed10 = createTemplate.HTMLBody.Replace("{10}",tb10.Text);
    createTemplate.HTMLBody = changed10;
            
    var changed11 = createTemplate.HTMLBody.Replace("{11}",tb11.Text);
    createTemplate.HTMLBody = changed11;
    /* END Replace Text from Textbox Text Property */
            
    createTemplate.Display(createTemplate);
            
}

Numeric Up Downs
nud0 to nud5 are default numericUpDowns with a max of 9999. nud6 has been modified to have 1 decimal place and has a max of 100.

Textboxes
tb7 to tb11 are all default, limited to 15 characters. Characters used include A-Z and 0-9.

.OFT Template Example
Template Body

3 Answers

I've reproduced the part of your project.

But it is working.

My project and .oft file are uploaded at Google Drive.

You can run and test.

Check my solution if you've done any silly mistakes at yours.

If still doesn't work then...

Ultimate Solution:

Possibly it is because of Visual Studio's stupid caching or another issue.

Resolve attempt:

  1. Try cleaning your solution and then rebuild.
  2. If doesn't work, then close all instances of Visual Studio and then start again.
  3. If still it doesn't work, then restart your PC.

The Replace function returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string, using the provided comparison type which is not specified. I'd suggest checking the actual values used for the Replace function and use the comparison type which specifies the culture, case, and sort rules to be used by certain overloads of the Compare(String, String) and Equals(Object) methods.

Another aspect is that you don't get a well-formatted HTML string returned from the Replace function. So, therefore it fails to update the message body. What value do you want set instead of placeholders?

Related