保存word doc C时出错 [英] Error when saving word doc C#

查看:74
本文介绍了保存word doc C时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我目前有一个合并单个单词的程序。用户选择的文档。用户有5个输入选项,允许选择文件。然后,combine按钮调用MsMerge.cs并将选定的文件合并为一个。以下是命令的示例



Hello
i currently have a program that merges individual word. documents as selected by the user. The user has 5 input options which allows selection of files. The combine button then calls the MsMerge.cs and combines the files selected into one. Below is an example of the command

private void combineButton2_Click(object sender, EventArgs e)
       {
           List <string> docList = new List<string>();
           docList.Add(selectedFile1);
           docList.Add(selectedFile2);
           docList.Add(selectedFile3);
           docList.Add(selectedFile4);
           docList.Add(selectedFile5);

           if (outputFolder2 != @"")
           {
               loadingForm.Show(); // To show the form

           string fileDate = DateTime.Now.ToString("dd-MM-yy");
           string fileTime = DateTime.Now.ToString("HH.mm.ss");
           string outcomeFolder2 = outputFolder2;
           string outputFile2 = "Combined Files " 
                               + fileDate + " @ " + fileTime + ".docx";
           string outputFileName2 = Path.Combine(outcomeFolder2, outputFile2);

           MsWord.Merge(docList.ToArray(), outputFileName2, true);

           loadingForm.Hide(); // to hide the form

           // Message displaying how many files are combined.
           MessageBox.Show("A total of " + docList.Count + " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }





我遇到的问题是当这个'合并'序列完成时,我会看到想要保存对文档的更改 - 好像程序未正确保存/组合所选文件。由于这个原因,我认为上面显示的'List'代码和MsWord.cs之间存在冲突:



the problem i am having is when this 'merge' sequence is finished, i am presented with a "want to save changes to your document" - as if the program isn't correctly saving / combining the files selected. Due to this i think there is a clash between 'List' code shown above and the MsWord.cs:

  public class MsWord
    {
        private static string defaultWordDocumentTemplate = @"Normal.dot";
       
        public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks)
        {
            Merge(filesToMerge, outputFilename, insertPageBreaks, defaultWordDocumentTemplate);
        }

        public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
        {
            object defaultTemplate = documentTemplate;
            object missing = System.Type.Missing;
            object pageBreak = Word.WdBreakType.wdSectionBreakNextPage;
            object outputFile = outputFilename;

            // Create a new Word application
            Word._Application wordApplication = new Word.Application();

            try
            {
                // Create a new file based on our template
                Word.Document wordDocument = wordApplication.Documents.Add(
                                              ref missing
                                            , ref missing
                                            , ref missing
                                            , ref missing);

                // Make a Word selection object.
                Word.Selection selection = wordApplication.Selection;

                // Count the number of documents to insert;
                int documentCount = filesToMerge.Length;

                // A counter that signals that we shoudn't insert a page break at the end of document.
                int breakStop = 0;

                // Loop thru each of the Word documents
                foreach (string file in filesToMerge)
                {
                    breakStop++;
                    // Insert the files to our template
                    selection.InsertFile(
                                                file
                                            , ref missing
                                            , ref missing
                                            , ref missing
                                            , ref missing);

                    //Do we want page breaks added after each documents?
                    if (insertPageBreaks && breakStop != documentCount)
                    {
                        selection.InsertBreak(ref pageBreak);
                    }
                }

                // Save the document to it's output file.
                wordDocument.SaveAs(
                                ref outputFile
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing
                            , ref missing);

                // Clean up!
                wordDocument = null;
            }

            catch (Exception ex)
            {
                // need to add handler
            }
            finally
            {
                // Finally, Close our Word application
                wordApplication.Quit(ref missing, ref missing, ref missing);
            }
        }
    }
}





我一直在努力寻找是什么导致了冲突,但到目前为止还没有这样做。任何帮助将不胜感激



更新:

弹出窗口时发现错误代码,

异常抛出:System.Runtime.InteropServices.COMException



我尝试过:



最初将combineButton绑定为合并string []数组而不是部分工作的列表,因为它允许合并5个文档,但是它需要选择所有5个用户输入作为选择2/3/4文件会导致程序崩溃。这导致我将我的设计从String数组改为List



i've been trying to find what is causing the clash but so far have failed to do so. Any help would be greatly appreciated

Update:
discovered error code when popup appears,
Exception thrown: System.Runtime.InteropServices.COMException

What I have tried:

initially had the combineButton rigged up to merge a string [] array as opposed to a list which partially worked as it allowed 5 documents to be combined, however it required all 5 user inputs to be selected as choosing 2/3/4 files would cause the program to crash. This then led me to change my design from String array to a List

推荐答案

终于得到了一个有效的解决方案,

该程序的问题,如上所述,merge命令每次都在查找5个文件,即使列表中的选择为null也是如此。



finally got a working solution,
the issue with the program, as mentioned was the merge command was looking for 5 files every time, even if the selections in the list were null.

private void combineButton2_Click(object sender, EventArgs e)
    {
        var docList = new List<string>();

     // if statements to determine if the file is added
        if (selectedFile1 != null)
        {
            docList.Add(selectedFile1);
        };

        if (selectedFile2 != null)
        {
            docList.Add(selectedFile2);
        };

        if (selectedFile3 != null)
        {
            docList.Add(selectedFile3);
        };

        if (selectedFile4 != null)
        {
            docList.Add(selectedFile4);
        };

        if (selectedFile5 != null)
        {
            docList.Add(selectedFile5);
        };

        string[] docListString = docList.ToArray();

        if (outputFolder2 != @"")
        {
            loadingForm.Show(); // To show the form

        string fileDate = DateTime.Now.ToString("dd-MM-yy");
        string fileTime = DateTime.Now.ToString("HH.mm.ss");
        string outcomeFolder2 = outputFolder2;
        string outputFile2 = "Combined Files " + fileDate + " @ " + fileTime + ".docx";
        string outputFileName2 = Path.Combine(outcomeFolder2, outputFile2);

        MsWord.Merge(docListString, outputFileName2, true);

        loadingForm.Hide(); // to hide the form

        // Message displaying how many files are combined. 
        MessageBox.Show("The " + docListString.Length.ToString() + " individual Documents selected have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }







包含if语句有助于确定文件是否已被选中。如果'selectedfile'不为null,则将其添加到List中,这可以防止程序搜索尚未选择的文档。




the inclusion of if statements helps to determine if a file has been selected. if the 'selectedfile' is not null, it is THEN added to the List, this prevents the program searching for documents that haven't been selected.


这篇关于保存word doc C时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆