关于c#生成word的问题 System.Runtime.InteropServices.COMException
System.Runtime.InteropServices.COMException”类型的未经处理的异常在 ModelPrint.exe 中发生
其他信息: 对 COM 组件的调用返回了错误 HRESULT E_FAIL。
private void querendan_Click(bool printModel, string printName, string hetongId, string hetongName, string hetongContect, string hetongTel, string hetongCode, string hetongType, string hetongSeal)
{
this.KillProcess("WINWORD");
string path = System.IO.Directory.GetCurrentDirectory();
Microsoft.Office.Interop.Word.Application app = null;
Microsoft.Office.Interop.Word.Document doc = null;
string dotFilename = (hetongType == "1") ? @"\querenTem1.dot" : @"\querenTem2.dot";
object missing = System.Reflection.Missing.Value;
object templateFile = path + dotFilename;
app = new Microsoft.Office.Interop.Word.ApplicationClass();
doc = app.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);
string printer = app.ActivePrinter;
app.ActivePrinter = (printName == "1") ? lstPrinter.Text : lstPrinter1.Text;
//声明书签数组
object[] oBookMark = new object[4];
oBookMark[0] = "hetongCode";
oBookMark[1] = "hetongContect";
oBookMark[2] = "hetongName";
oBookMark[3] = "hetongTel";
//赋值数据到书签的位置
doc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = hetongCode;
doc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = hetongContect;
doc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = hetongName;
doc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = hetongTel;
if (checkBox2.Checked == true && printName == "2")
{
string pngPath = label6.Text + @"\" + hetongId + ".png";
if (File.Exists(pngPath))
{
Random rd = new Random();
//要插入的图片文件
app.Selection.InlineShapes.AddPicture(pngPath, ref missing, ref missing, ref missing);
//将图片设置为四周环绕型
Microsoft.Office.Interop.Word.Shape cShape = doc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
cShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapTopBottom;
cShape.Left = rd.Next(350, 400);
cShape.Top = rd.Next(500, 550);// WdShapePosition.wdShapeCenter;居中
cShape.Rotation = rd.Next(100, 360);
//设置图片浮于文字之上 - 查阅WdWrapType的相关WdWrapType Enumeration
cShape.WrapFormat.Type = WdWrapType.wdWrapNone;
}
}
if (printModel || comboBox1.Text.Equals("打印"))
{
//打印
doc.PrintOut(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, ref missing, ref missing, ref missing);
//MessageBox.Show("确认单打印成功","提示");
}
else
{
//弹出保存文件对话框,保存生成的Word
app.Visible = false;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Word Document(*.doc)|*.doc";
sfd.DefaultExt = "Word Document(*.doc)|*.doc";
if (sfd.ShowDialog() == DialogResult.OK)
{
object filename = sfd.FileName;
doc.SaveAs(ref filename, 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);
doc.Close(ref missing, ref missing, ref missing);
//关闭word
app.Quit(ref missing, ref missing, ref missing);
MessageBox.Show("确认单导出成功", "提示");
}
}
}
相关推荐
C#读写Word文档异常处理解析System.Runtime.InteropServices.COMException
### C#读取Word文档异常处理解析:System.Runtime.InteropServices.COMException#### 一、引言在使用C#进行Web应用开发过程中,经常会遇到需要读取或操作
word内嵌
**错误处理**:由于Word是外部进程,我们需要妥善处理可能的异常,例如文件未找到或Word未安装的情况: ```csharp try { // 代码块 } catch (System.Runtime.InteropServices.COMException
JEP30-PX100A PartModel Product Guidelines for Electronic-Device Substrates and Assemblies – XML Requ
JEP30-PX100A PartModel Product Guidelines for Electronic-Device Substrates and Assemblies – XML Requirements
基于Matlab的自动睡眠粒状结构检测算法,从脑电信号中进行。.zip
1.版本:matlab2014a/2019b/2024b 2.附赠案例数据可直接运行。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
JEP375 2.5D_3D Silicon thinned wafers with Through Silicon Vias (TSV’s) and Backside Redistribution
JEP375 2.5D_3D Silicon thinned wafers with Through Silicon Vias (TSV’s) and Backside Redistribution Layer (RDL).pdf


