如何在现有文档中插入封面和分页符?

问题描述

要做的任务是将封面文字文档的内容插入到一组现有的文字文档中。现有文档全部位于ToBeProcessed文件夹中,封面存储在其自己的文件夹中。我创建的过程是将封面的内容存储为字符串,然后遍历打开每个文件的现有文件列表,使用范围选择文档的开头,插入文本,然后添加分页符。>

到目前为止,除了分页符被插入到封面页面内容之前的事实之外,一切都按预期进行。有人可以建议如何实现吗?万一有人有以更有效的方式获得相同结果的经验,我已经包含了程序的全部内容,但是从我的研究来看,使用范围似乎是最佳实践。

        const string COVERING_FOLDER = @"C:\Users\alex.grimsley1\Documents\DocmanLetters\CoveringPage";
        const string COMPLETE_FOLDER = @"C:\Users\alex.grimsley1\Documents\DocmanLetters\Complete";
        const string LOG_FOLDER = @"C:\Users\alex.grimsley1\Documents\DocmanLetters\Log";

        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss"));
            StreamWriter logfile = new StreamWriter(LOG_FOLDER + @"\Log_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + @".txt");
            
            //Returns an array of strings which contains the list of filenames to be edited
            var files = Directory.GetFiles(PROCESSING_FOLDER);

            //Creates a new Application wrapper
            Application ap = new Application();

            //Open the Covering Page document
            var cp = ap.Documents.Open(COVERING_FOLDER + "\\CoveringPage.docx");

            //copy the Covering Page contents to a variable for later use
            var cpContent = cp.Content.Text;

            //Close the Covering Page document
            cp.Close(WdSaveOptions.wdDoNotSaveChanges);

            //Loop through the file list
            foreach(var file in files)
            {
                if(file.Substring(0,PROCESSING_FOLDER.Length + 1) == PROCESSING_FOLDER + "~")
                {
                    Console.WriteLine("Skipping temporary file: " + file);
                } else
                {
                    try
                    {
                        object missing = System.Type.Missing;
                        
                        //Open each file using the Application wrapper
                        var d = ap.Documents.Open(file);
                        ap.Visible = true;
                        var dName = d.Name;

                        //Select a range at the very start of the Document
                        var rng = d.Range(0,0);
                        d.Unprotect();
                        d.Protect(WdProtectionType.wdnoprotection);

                        //Add covering page text to the Document
                        rng.Text = cpContent;
                        rng.Collapse(WdCollapseDirection.wdCollapseEnd);
                        rng.InsertBreak(WdBreakType.wdPageBreak);

                        //Write the Document name to the Console
                        Console.WriteLine(d.Name);

                        //Save the document in place
                        d.Save();

                        //Close the document
                        d.Close(WdSaveOptions.wdDoNotSaveChanges);

                        //Move the file to the Complete folder
                        File.Move(file,COMPLETE_FOLDER + "\\" + dName);

                        //Log the completion of the letter
                        logfile.WriteLine("Complete " + DateTime.Now.ToString() + " - " + d.Name);
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine("There was an error: " + e.Message);

                        //Log the completion of the letter
                        logfile.WriteLine("Error " + DateTime.Now.ToString() + " - " + e.Message);
                    }
                    finally
                    {
                        // Close Word application
                        ap.Quit();
                        Marshal.ReleaseComObject(ap);
                        ap = null;
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                }
                           
            }

            
            Console.WriteLine("Processing complete...");
            Console.ReadLine();

        }```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)