2016-03-22 106 views
0

我目前有一個合併單個單詞的程序。由用戶選擇的文檔。用戶有5個輸入選項可以選擇文件。組合按鈕然後調用MsMerge.cs並將所選文件合併爲一個。下面是保存word文檔C時出錯#

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 loading 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 loading form 

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

我遇到的問題是,當這種「合併」序列完成後,我提出了一個「想更改保存到文檔」​​命令的例子 - 因爲如果程序ISN」 t正確保存/組合選定的文件。由於這一點,我認爲有}上面顯示「列表」代碼和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); 
     } 
    } 

我最初有「combineButton」裝配了合併的字符串[]數組而不是一個部分工作的列表,因爲它允許組合5個文檔,但是它需要選擇所有5個用戶輸入。如果用戶選擇2/3/4文件,程序將崩潰。這導致我將我的設計從String數組更改爲List。

我一直在試圖找出造成衝突的原因,但至今未能如願。任何幫助將不勝感激

** 更新1

似乎得到錯誤 拋出異常:「System.Runtime.InteropServices.COMException」 當程序引發了另存爲彈出。

** 更新2

計劃將合併在5個選項的文件製成。如果選擇了最多5個文件,合併過程將正常工作,但是如果選擇了2/3/4文件,在選擇中留下空位 - 將出現彈出錯誤,導致我認爲空閒瀏覽路徑與保存過程。

+0

outputFile的值是什麼? combineButton2中的 –

+0

?它只是正在創建的文件的名稱,即組合文件+時間戳。然後path.combined與用戶選擇的輸出文件夾,它被標記爲outputFolder2 – cgraham720

+0

我正在談論這(可能)導致錯誤的這條線; wordDocument.SaveAs(ref outputFile ....)。試着硬編碼完整的路徑而不是outputFile,看看它是否工作。最好的問候, –

回答

0

如上所述,發現如果5個列表項被插入,但2/3/4卻沒有,這表明缺少文件導致了衝突。然後我說,如果語句到「文檔」添加到列表中,如果他們不爲空,這意味着它只會合併,而不是尋找5個假設文件選擇什麼:

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); 
     } 

代碼是升技原油但基本知識都在那裏,只需要清理它並添加額外的語句以確保必須選擇多於一個的文件才能合併等等。

謝謝大家的建議!

1

終於拿到了程序,沒有彈出錯誤上班/另存爲錯誤,純粹是從試驗和錯誤

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

     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); 

基本上我在按鈕的代碼改變了List docListVar docList,我再變重新手動列表回到一個字符串,而不是使用.ToArray.

我不知道爲什麼以前的代碼不起作用,但是一定有一些衝突與列表和MsWord.cs導致錯誤。感謝@AlexBell和@KirillShlenskiy的建議。

+0

虛驚一場。程序沒有編譯文件 - 因此錯誤沒有出現。 – cgraham720

+0

如果使用List是問題,但Array有調整大小的方法,我會感到驚訝。我只是想到了你需要編寫的代碼來確定哪些索引被填充比使用List和調用ToArarry() – Crowcoder

+0

@crowcoder更加尷尬 - 在弄亂了代碼後,我發現了「popup」消息關於將文件保存爲名稱僅在5個瀏覽文件中的一個留空時纔會出現。例如,如果我選擇5個單獨的文件併合並。該計劃的作品。但是,如果我選擇2/3/4,彈出窗口出現。就好像缺少文檔會破壞儲蓄過程一樣。 – cgraham720