110,561
社区成员
发帖
与我相关
我的任务
分享
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop;
using Microsoft.Office.Core;
DocumentWriter doc = new DocumentWriter(TemplateFile);
for (i = 0; i < pc.Count; i++)
{
try
{
doc.Write("bm1", pc[i].xx);//具体代码关系不大,先不贴了
Application.DoEvents();
doc.QuickPrint();
Application.DoEvents();
doc.SaveAs(filename);//具体代码关系不大,先不贴了
}
catch (Exception e)
{
doc.CloseAll();
return -1;
}
Application.DoEvents();
}
doc.CloseAll();//这里出错
private void OpenDocument(string filename)
{
object FileName = filename;
wapp = new Microsoft.Office.Interop.Word.Application();
wapp.Visible = false;
wapp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
if (!File.Exists(filename))
{
try
{
doc = wapp.Documents.Add(MissingObject, MissingObject, MissingObject, MissingObject);
_filename = filename;
}
catch
{
throw new FileNotFoundException("无效文件路径");
}
}
else
{
_filename = "";
doc = wapp.Documents.Open(ref FileName, ref MissingObject, ref ReadOnly, ref MissingObject, ref MissingObject, ref MissingObject, ref MissingObject, ref MissingObject, ref MissingObject,
ref MissingObject, ref MissingObject, ref Visible, ref MissingObject, ref MissingObject, ref MissingObject, ref MissingObject);
}
doc.Activate();
System.Windows.Forms.Application.DoEvents();
}
public void QuickPrint()
{
object Background = true;
doc.PrintOut(Background, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject, MissingObject);
System.Windows.Forms.Application.DoEvents();
}
public void CloseAll()
{
if (wapp != null)
{
if (doc != null)
{
try
{
doc.Close(SaveChanges, MissingObject, MissingObject);
}
finally
{
doc = null;
}
}
try
{
if (wapp.Documents.Count == 0)
{
wapp.Quit(SaveChanges, MissingObject, MissingObject);
}
}
finally
{
wapp = null;
}
}
GC.Collect();
System.Windows.Forms.Application.DoEvents();
}
}