我目前有一個合併單個單詞的程序。由用戶選擇的文檔。用戶有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文件,在選擇中留下空位 - 將出現彈出錯誤,導致我認爲空閒瀏覽路徑與保存過程。
outputFile的值是什麼? combineButton2中的 –
?它只是正在創建的文件的名稱,即組合文件+時間戳。然後path.combined與用戶選擇的輸出文件夾,它被標記爲outputFolder2 – cgraham720
我正在談論這(可能)導致錯誤的這條線; wordDocument.SaveAs(ref outputFile ....)。試着硬編碼完整的路徑而不是outputFile,看看它是否工作。最好的問候, –