Hello,
I have a word document (.docx) which has text boxes in it. Within each text box is a text placeholder which I replace (for example [CUSTOMER] gets replaced with the customer name). The code I have used works fine but when I have to use line breaks I just cannot figure out a way to add a line break. I have tried \v and environment.newline both of which result in a unhandled exception hexadecimal value 0x0b is an invalid character. Could anybody help and advise me how I can insert a line break into a textbox within the Word Document correctly?
public Boolean CreateJobTicket(String sTemplateFile, String sJobTicketFile, DataRow rowJobTicket, DataTable dtJobTicketQuantities, DataTable dtJobTicketMaterials, DataTable dtJobTicketTasks)
{
// classes called within this method
CommonConversions clsConv = new CommonConversions();
ConfigurationHandling clsConf = new ConfigurationHandling();
ErrorHandling clsError = new ErrorHandling();
// create the temp word document file
try
{
File.Copy(System.Configuration.ConfigurationManager.AppSettings["templatesFolder"].ToString() + sTemplateFile, sJobTicketFile, true);
}
catch (Exception ex)
{
clsError.RecordErrorDetail(clsConf.ReturnUserURN(), "JobTicketDocument.JobTicket.CreateJobTicket", "The attempt to create the copy word document failed with the error " + ex.Message.ToString());
return false;
}
// open the temporary word document for editing
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(sJobTicketFile, true))
{
var vWordDocument = wordDoc.MainDocumentPart.Document;
// loop through the word document text frames and replace the placeholder text items with the text from the
// datasources used for the job ticket
foreach (var vWordDocText in vWordDocument.Descendants())
{
if (vWordDocText.Text.Contains("[PROCESS]"))
{
vWordDocText.Text = vWordDocText.Text.Replace("[PROCESS]", ReturnJobTicketTasks(dtJobTicketTasks, 0));
}
}
}
return true;
}
public String ReturnJobTicketTasks(DataTable dtJobTicketTasks, Int16 iType)
{
String sJobTicketTasks = "";
foreach(DataRow rowTask in dtJobTicketTasks.Rows)
{
switch (iType)
{
case 0:
sJobTicketTasks = sJobTicketTasks + rowTask["TASK_NAME"].ToString();
if (Convert.ToInt32(rowTask["ESTIMATED_TIME"].ToString()) != 0)
{
if (Convert.ToInt32(rowTask["ESTIMATED_TIME"].ToString()) < 10)
{
sJobTicketTasks = sJobTicketTasks + " (" + rowTask["ESTIMATED_TIME"].ToString() + " min)";
}
else
{
sJobTicketTasks = sJobTicketTasks + " (" + rowTask["ESTIMATED_TIME"].ToString() + " mins)";
}
}
break;
case 1:
sJobTicketTasks = sJobTicketTasks + rowTask["PROCESS_NOTES"].ToString();
break;
}
sJobTicketTasks = sJobTicketTasks + "\n\r";
}
return sJobTicketTasks;
}