我有一个包含单个页面的word文档,我想将其用作模板来创建另一个word文档。新文档将包含多个页面,其中包含模板页面中替换的内容。
**Document 1**
Page 1 (Template Page)
**Document 2**
Page 1 (Copy of Template Page)
Page 2 (Copy of Template Page)
Page 3 ...
现在的模板只包含“Test#”行,我想用当前页码替换“#”。我当前的代码如下,应该生成一个包含两个页面的新文档。
string filename = @"C:\xxxxx_in.docx";
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oDoc2 = oWord.Documents.Add();
for (int i = 1; i < 3; i++)
{
Microsoft.Office.Interop.Word.Document oDoc1 = oWord.Documents.Open(filename);
object matchCase = false;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
object findText = "#";
object replaceWithText = i.ToString();
oDoc1.Content.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
Microsoft.Office.Interop.Word.Range oRange = oDoc1.Content;
oRange.Copy();
oDoc2.Range(oDoc2.Content.End - 1, oDoc2.Content.End - 1).Paste();
oDoc1.Close();
}
object outputFileName = @"C:\xxxxx_out.docx";
oDoc2.SaveAs(ref outputFileName);
oWord.Quit();
问题是,我正在为该行获得例外...
oDoc2.Range(oDoc2.Content.End - 1, oDoc2.Content.End - 1).Paste();
System.Runtime.InteropService.COMException:“调用的对象已与其客户端断开连接。”
我怎样才能解决这个问题?谢谢您的帮助!
我通过在单独的单词应用程序中打开第二个文档来解决问题。这是工作代码。
string filename = @"C:\xxxxx_in.docx";
Microsoft.Office.Interop.Word.Application oWord1 = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Application oWord2 = new Microsoft.Office.Interop.Word.Application();
int pages = 3;
try
{
Microsoft.Office.Interop.Word.Document oDoc2 = oWord2.Documents.Add();
for (int i = 0; i < pages; i++)
{
Microsoft.Office.Interop.Word.Document oDoc1 = oWord1.Documents.Open(filename);
object matchCase = false;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
object findText = "#";
object replaceWithText = i.ToString();
oDoc1.Content.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
Microsoft.Office.Interop.Word.Range oRange = oDoc1.Content;
oRange.Copy();
oDoc2.Range(oDoc2.Content.End - 1, oDoc2.Content.End - 1).Paste();
if (i + 1 < pages)
{
oDoc2.Range(oDoc2.Content.End - 1, oDoc2.Content.End - 1).InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak);
}
oDoc1.Close(false);
}
object outputFileName = @"C:\xxxxx_out.docx";
oDoc2.SaveAs(ref outputFileName);
oDoc2.Close(false);
}
catch (Exception ex)
{
// Do something.
}
finally
{
oWord1.Quit(false);
oWord2.Quit(false);
}
问题中代码的问题是oDoc1
正在为循环中的每次传递实例化。
Microsoft.Office.Interop.Word.Document oDoc1 = oWord.Documents.Open(filename);
把它放在for
循环之前,这样文档只打开一次。然后oDoc1
不应该断开连接。
更好的方法是使用Documents.Add
从“模板”创建新文档(oDoc1
)。这样就不会有模板文件被更改的危险。然后,为了重复“空”页面(在替换值之前的内容),尝试使用Range.InsertFile
方法在oDoc1
结尾处引入“模板”的内容。