![]() |
#1
|
|||
|
|||
![]()
Hi,
I have word template document having some merge fields. One merge field have multiple values which i want to show each value with line break. For example: Value1 Value2 Value3 Here is my code, when document is created and download values are not shows with line break they shows with whitespace between them. Code:
private void setupDocument() { try { string documentLanguage = language.Equals("English") ? "English" : "Swedish"; docName = "~/Media/bookingDocs/" + documentLanguage + "_Doc_" + bookingID + ".docx"; string generatedFile = HttpContext.Current.Server.MapPath(docName); File.Copy(getTemplateFilePath(), generatedFile, true); Dictionary<string, string> keyValues = new Dictionary<string, string>(); keyValues.Add("MainRoomName", doc.MainRoom); keyValues.Add("BookingStartEndTime", doc.BookingTime); keyValues.Add("TotalGuest", doc.TotalGuest); keyValues.Add("BreakfastServingTime", doc.BreakfastServingTime); keyValues.Add("LunchServingTime", doc.LunchServingTime); keyValues.Add("CoffeServingTime", doc.CoffeServingTime); keyValues.Add("CompanyName", doc.CompanyName); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(generatedFile, true)) { var mergeFields = wordDoc.MainDocumentPart.RootElement.Descendants<FieldCode>(); foreach (KeyValuePair<string, string> keys in keyValues) { ReplaceMergeFieldWithText(mergeFields, keys.Key, keys.Value); } // update lunch room merge field ReplaceMergeFieldLunchRooms(mergeFields, "LunchRoomName", doc.ListOfLunchRooms); // update mergeField for specialFood ReplaceMergeFieldSpecialFood(mergeFields, "BookingSpecialFoodList", doc.ListOfSpecialFood); wordDoc.MainDocumentPart.Document.Save(); } } catch (Exception exc) { log.Error(userName + ": Exception in preparePrintDocument setupDocument: " + exc.Message); log.Error(userName + ": Exception in preparePrintDocument setupDocument: " + exc.StackTrace); throw new Exception(exc.Message); } } private void ReplaceMergeFieldLunchRooms(IEnumerable<FieldCode> fields, string mergeFieldName, List<System.Web.UI.WebControls.ListItem> lunchRoomList) { try { var field = fields .Where(f => f.InnerText.Contains(mergeFieldName)) .FirstOrDefault(); if (field != null) { // Get the Run that contains our FieldCode // Then get the parent container of this Run Run rFldCode = (Run)field.Parent; // Get the three (3) other Runs that make up our merge field Run rBegin = rFldCode.PreviousSibling<Run>(); Run rSep = rFldCode.NextSibling<Run>(); Run rText = rSep.NextSibling<Run>(); Run rEnd = rText.NextSibling<Run>(); // Get the Run that holds the Text element for our merge field // Get the Text element and replace the text content Text t = rText.GetFirstChild<Text>(); System.Text.StringBuilder sb = new StringBuilder(); if (lunchRoomList.Count > 0) { foreach (System.Web.UI.WebControls.ListItem item in lunchRoomList) { sb.Append(item.Text); sb.Append(Environment.NewLine); } } t.Text = sb.ToString(); // Remove all the four (4) Runs for our merge field rFldCode.Remove(); rBegin.Remove(); rSep.Remove(); rEnd.Remove(); } } catch (Exception exc) { log.Error(userName + ": Exception in preparePrintDocument ReplaceMergeFieldWithText: " + exc.Message); log.Error(userName + ": Exception in preparePrintDocument ReplaceMergeFieldWithText: " + exc.StackTrace); throw new Exception(exc.Message); } } |
#2
|
||||
|
||||
![]()
It's not apparent to me why you'd be creating the document from scratch every time instead of using a document or template with the required fields already in place. That would require much less code and would be far easier to maintain.
As for changing a mergefield's output from: Value1 Value2 Value3 to: Value1 Value2 Value3 there is no mergefield coding that can produce such a result.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Hi macropod,
Thanks for the replay. basically i am create a document for events. So i have template doc with merge field. Which will be replace with actual information and new document is save for the event. each event have different information so document should be created from the scrach for the each event. |
#4
|
||||
|
||||
![]() Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
Yes you are right there is no need to use mailmerge field in my example code.
My requirement is i have template document with some placeholder where i can put the dynamic data. For example i want to add User name and address. So i need a place holder to know where is user name in the document that i should replace with real user namer. There are multiple ways to do such use some sort of tokens and just replace such as ###USERNAME### or as you suggest bookmarks etc. Unfortunatly first example i found at internet regarding my requirment is with mailMarge so i used that. |
![]() |
Tags |
line breaking, mergefield |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
GreenBoy | Word | 2 | 03-11-2018 02:32 AM |
![]() |
alan100 | Mail Merge | 5 | 12-12-2017 08:32 PM |
Adding a carriage return at end of each line, and then a blank space, after text entry complete | CEMurphy4 | Word VBA | 1 | 12-02-2015 11:53 PM |
How to automatically insert a line after a Column Break | YooperNC | Word | 3 | 01-29-2015 07:16 AM |
![]() |
camlad | Mail Merge | 3 | 06-29-2011 05:01 PM |