关于c#生成word的问题 System.Runtime.InteropServices.COMException

wxcyz wxcyz 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
单个或者批量执行中的第一条没有问题,只要cShape 用过一次就报错误。

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("确认单导出成功", "提示");
}
}
}
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
wxcyz
C# 111,128 成员 642,536 内容