Добавьте поле в текстовый документ, используя add in, используя Microsoft.Office.Interop.Word C # - PullRequest
0 голосов
/ 30 мая 2019

Я создал простое слово add, которое вставляет некоторый текст в документ word,

using Microsoft.Office.Interop.Word

Я хочу добавить свои пользовательские поля в поля вставки из модели C # Пример

public class test (){
public int id {get;set;}
public int Name{get;set;}
}



 public string ExtractText()
        {
            string filename = RandomString(10);


            #region Create copy of doc 
            var app = new Microsoft.Office.Interop.Word.Application();

           //save document to the same location with different name 
            var path2 = System.IO.Path.Combine(
                 Directory.GetCurrentDirectory(), "Documents", filename.ToString().Replace(".docx", "") + "V2" + ".docx");
            System.IO.File.Create(path2).Close();
            Document document = app.Documents.Open(path2);
            #endregion

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)
                    data.Add(temp);
            }
            #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");
            #endregion
            object missing = System.Reflection.Missing.Value;
            //Enable modification
            app.ActiveDocument.Content.Editors.Add(Microsoft.Office.Interop.Word.WdEditorType.wdEditorEveryone);

            //List<int> paragraphsPermission = new List< int>();
            // List<Tuple<int, string>> paragraphsPermission = new List<Tuple<int, string>>();

            List<int> paragraphsPermission = new List<int>();
            //Add text to doc
            foreach (var item in data)
            {

                var para = document.Content.Paragraphs.Add(ref missing);

                document.MailMerge.Fields.Add(para.Range ,"Name");


                para.Range.Text = item.Trim();
                para.Range.InsertParagraphAfter();
                #region Remove this after implementation  
                if (item.Contains("Second"))
                {
                    paragraphsPermission.Add(1);
                }
                else if (item.Contains("Next LINE1"))
                {
                    paragraphsPermission.Add(1);
                }
                else
                {
                    paragraphsPermission.Add(0);
                }
                #endregion
            }
            #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)
                    document.Paragraphs[nextparagraphs].Range.Editors.Add(WdEditorType.wdEditorEditors);
                else
                    document.Paragraphs[nextparagraphs].Range.Editors.Add(WdEditorType.wdEditorEveryone);
                nextparagraphs++;
            }

            document.Save();
            document.Close();

поэтому, когда пользователь нажимает на вкладку, поданную во вкладке Mail merger, он должен показывать его идентификатор и имя

В настоящее время он отключает вкладку почтовых сообщений, я хочу включить ее из C # enter image description here

, поэтому идентификатор и имя должны отображаться в поле слияния вставки

enter image description here

Спасибо

...