Sorry for the chronic name, but could not think of a better way to say this, and you have Bingleing for several days.
I have code that reads in an RTF file, which I put with placeholders as merge fields. The code will replace the text, but when the file is saved, the new values will not be displayed.
var fileName = @"C:\Users\James\SkyDrive\Sandbox\MailMerge\MailMerge\templates\Template1.rtf"; StreamReader sr = new StreamReader(fileName); string text = sr.ReadToEnd(); sr.Close(); text = text.Replace("{\\{Title\\}\\}", "Mr."); text = text.Replace("{\\{LastName\\}\\}", "Johnson"); text = text.Replace("{\\{FirstName\\}\\}", "James");
RTF before replacement: {\ rtf1 \ ansi \ ansicpg1252 \ deff0 \ nouicompat \ deflang1033 {\ fonttbl {\ f0 \ fnil \ fcharset0 Calibri;}} {* \ generator Riched20 6.2.9200} \ viewkind4 \ uc1 \ pard \ sa200 \ sl276 \ slmult1 \ f0 \ fs22 \ lang9 Hello {{Name}} {{LastName}} \ par How are you, {{FirstName}} \ par}
RTF after replacement: {\ rtf1 \ ansi \ ansicpg1252 \ deff0 \ nouicompat \ deflang1033 {\ fonttbl {\ f0 \ fnil \ fcharset0 Calibri;}} {* \ generator Riched20 6.2.9200} \ viewkind4 \ uc1 \ pard \ sa200 \ sl276 \ slmult1 \ f0 \ fs22 \ lang9 Hello \ Mr. \ Johnson \ n How are you, \ James \ par}
RTF after saving and opening in notepad: {\ rtf1 \ ansi \ ansicpg1252 \ deff0 \ deflang1033 {\ fonttbl {\ f0 \ fnil \ fcharset0 Calibri;}} \ viewkind4 \ uc1 \ pard \ sa200 \ sl276 \ slmult1 \ lang9 \ f0 \ fs22 Hello. \ n How do you do this, \ par}
Is there another step I need to take that will save the file in the correct format? I also tried using the RichTextBox control by loading a string into it and then calling the Save () method. this gives me the same results.
Thanks James
After reading this post, I cleared the RTF text and using the following code:
var fileName = @"C:\Users\James\SkyDrive\Sandbox\MailMerge\MailMerge\templates\Template1.rtf"; StreamReader sr = new StreamReader(fileName); string text = sr.ReadToEnd(); sr.Close(); rtb.Rtf = text; rtb.Text = rtb.Text.Replace("{{Title}}", "Mr."); rtb.Text = rtb.Text.Replace("{{LastName}}", "Johnson"); rtb.Text = rtb.Text.Replace("{{FirstName}}", "James"); rtb.SaveFile(@"C:\Users\James\SkyDrive\Sandbox\MailMerge\MailMerge\templates\Johnson2.rtf");