我有一个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:“调用的对象具有 与客户断开连接。”
我该如何解决?感谢您的帮助!
答案 0 :(得分:0)
我通过在单独的Word应用程序中打开第二个文档来解决此问题。这是工作代码。
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);
}
答案 1 :(得分:0)
问题代码的问题在于,oDoc1
在每次循环中都被实例化。
Microsoft.Office.Interop.Word.Document oDoc1 = oWord.Documents.Open(filename);
将此{em>放在之前,放在for
循环中,以使文档仅打开一次。这样oDoc1
就不会断开连接。
更好是将使用Documents.Add
从“模板”创建新文档(oDoc1
)。这样就没有更改模板文件的危险。然后,为了重复“空”页面(替换值之前的内容),请尝试使用Range.InsertFile
方法在{{1的末尾引入“模板”的内容。 }}。