Add custom placeholders or mail merge fields to word document in using Microsoft.Office.Interop.Word C# RRS feed

  • Question

  • The issue i am having when i am adding the field to the mail merge it not showing that field in the drop down how to add custom Mail merger fields from Model C#

                    string filename = RandomString(10);

                    #region Create copy of doc 
                    var app = new Microsoft.Office.Interop.Word.Application();
                    //Conversting the b64 to file
                    var path2 = System.IO.Path.Combine(
                         Directory.GetCurrentDirectory(), "Documents", filename.ToString().Replace(".docx", "") + "V2" + ".docx");

                    Document document = app.Documents.Open(path2);

                    object missing = System.Reflection.Missing.Value;



                    //Loop between Paragraphs and add more Paragraphs
                    // ********************************************************************************
                    //----Loop between Paragraphs and add more Paragraphs ----- 
                    String read = string.Empty;
                    List<string> data = new List<string>();
                    for (int i = 0; i < document.Paragraphs.Count; i++)
                        string temp = document.Paragraphs[i + 1].Range.Text.Trim();
                        if (temp != string.Empty)
                    #region Add Paragraphs on backend 
                    // ********************************************************************************
                    //----Add Paragraphs on backend  ----- 
                    data.Add("Next LINE");
                    data.Add("Next LINE1");
                    data.Add("Second method opens the existing Microsoft Office Word document specified by a fully qualified path and file name. This method returns a Document that represents the opened document");
                    data.Add("Contrary <Department> to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney C");

                    List<int> paragraphsPermission = new List<int>();
                    //Add text to doc
                    foreach (var item in data)
                        var para = document.Content.Paragraphs.Add(ref missing);
                        //   var para = document.Content.Paragraphs.Add(ref missing);
                        para.Range.Text = item.Trim();
                        #region Remove this after implementation  
                        if (item.Contains("Second"))
                        else if (item.Contains("Next LINE1"))
                    #region Apply enforceStyle
                    app.Visible = true;
                    object noReset = true;
                    object password = System.String.Empty;
                    object useIRM = false;
                    object enforceStyleLock = true;
                    object PasswordEncryptionFileProperties = false;
             //       app.ActiveDocument.EnforceStyle = true;
         //           document.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading, ref noReset, "000", ref useIRM, ref enforceStyleLock);
                    int nextparagraphs = 1;
                    foreach (var item in paragraphsPermission)
                        if (item == 1)

                    Microsoft.Office.Interop.Word.Document doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
                    Microsoft.Office.Interop.Word.Range range = app.Selection.Range;

                    Microsoft.Office.Interop.Word.Field merge = app.ActiveDocument.Fields.Add(range, "JI");
                //    Microsoft.Office.Interop.Word.MailMergeField m1 = merge.Fields.Add(wrdSelection.Range,"lolTest");


                    document.Close();                                Issue it is not adding fields to the Mail merger tab                                                            

    • Edited by TalkWithMo Tuesday, June 4, 2019 8:06 PM
    Tuesday, June 4, 2019 7:57 PM