User181930479 posted
im trying to generate a word file based on template , and im replacing using the dictionary in c# , but if i have 2 keys , the word file is generated twice... :
below is my code :
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<string, string> d1 = new Dictionary<string, string>();
d1.Add("%APPLICANT%", "Bill");
d1.Add("%BBB%", "AIO");
foreach (var pair in d1)
{
CreateRejectionLetter(pair.Key, pair.Value);
}
}
private DocX GetRejectionLetterTemplate()
{
// path
string fileName = @"D:\DocXExample.docx";
// Create the document in memory:
var doc = DocX.Load(fileName);
return doc;
}
public void CreateRejectionLetter(string applicantField, string applicantName)
{
// We will need a file name for our output file (change to suit your machine):
string fileNameTemplate = @"D:\Rejection-Letter-{0}-{1}.docx";
// Let's save the file with a meaningful name, including the applicant name and the letter date:
string outputFileName = string.Format(fileNameTemplate, applicantName, DateTime.Now.ToString("MM-dd-yy"));
// Grab a reference to our document template:
DocX letter = this.GetRejectionLetterTemplate();
// Perform the replace:
letter.ReplaceText(applicantField, applicantName);
// Save as New filename:
letter.SaveAs(outputFileName);
// Open in word:
Process.Start("WINWORD.EXE", "\"" + outputFileName + "\"");
}