我目前有一个合并单个单词的程序。用户选择的文档。用户有5个输入选项,允许选择文件。然后,combine按钮调用MsMerge.cs并将选定的文件合并为一个。以下是命令的示例
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);
}
我遇到的问题是当这个'合并'序列完成时,我会看到“想要保存对文档的更改” - 好像程序没有正确保存/组合所选文件。由于这个原因,我认为上面显示的'List'代码和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'来合并一个string []数组而不是一个部分工作的列表,因为它允许组合5个文档,但是它需要选择所有5个用户输入。如果用户选择2/3/4文件,程序将崩溃。这导致我将我的设计从String数组更改为List。
我一直试图找到导致冲突的原因,但到目前为止还没有这样做。任何帮助将不胜感激
** 更新1
似乎越来越错误 抛出异常:'System.Runtime.InteropServices.COMException' 当程序抛出save-as弹出窗口时。
** 更新2
程序将合并文件中的5个选项。如果选择了最多5个文件,合并过程将正常工作,但如果选择了2/3/4文件,则在选择中留下空缺 - 将出现弹出错误,导致我认为空闲的浏览路径与之冲突保存过程。
答案 0 :(得分: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 docList
更改为Var docList
,然后手动将列表更改回字符串,而不是使用.ToArray.
我不确定为什么之前的代码不起作用,但是必须与列表和MsWord.cs发生冲突才会导致错误。感谢@AlexBell和@KirillShlenskiy的建议。
答案 1 :(得分:0)
如上所述,我们发现,如果插入了5个列表项,那么2/3/4没有工作,这表明缺少文件导致了冲突。然后我添加了if语句来添加&#39; docs&#39;列表如果它们不是null,意味着它只会合并所选的内容而不是查找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);
}
代码是abit crude但是基础知识都存在,只需要清理它并添加其他语句以确保必须选择多个文件进行合并等。
谢谢大家的建议!