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

wxcyz 2016-04-18 05:39:15
单个或者批量执行中的第一条没有问题,只要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("确认单导出成功", "提示");
}
}
}
...全文
646 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Strong08 2017-01-04
  • 打赏
  • 举报
回复
同样是第一次插入图片没有问题,第二次插入就出现此问题,代码如下 private bool writeword(string tempDoc, string FileName) { string templateFile = tempDoc; string fileNameWord = FileName; bool ret = false; Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); object Obj_FileName = fileNameWord; object Visible = false; object ReadOnly = false; object missing = System.Reflection.Missing.Value; try { File.Copy(templateFile, fileNameWord, true); doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref Visible, ref missing, ref missing, ref missing, ref missing); doc.Activate(); for (int i = 0; i < 2; i++) { doc.Tables[i+1].Select();//复制第一个表格,如果有多条粘贴到尾部 app.Selection.Copy();//如果导入多条要把原来的模版粘贴下来 object replaceArea = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; object replaceKey = "$devicename$"; object replaceValue = "装置名称"; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); replaceKey = "$areaname$"; replaceValue = "装置名称"; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); replaceKey = "$enquipmentname$"; replaceValue = "装置名称"; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); replaceKey = "$tagnumber$"; replaceValue = "装置名称"; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); replaceKey = "$componenttype$"; replaceValue = "装置名称"; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); replaceKey = "$medium$"; replaceValue = "装置名称"; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); replaceKey = "$emmisionderection$"; replaceValue = ""; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); replaceKey = "$detectvalue$"; replaceValue = "装置名称"; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); replaceKey = "$detectmandate$"; replaceValue = "装置名称"; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); replaceKey = "$repairdate$"; replaceValue = "10"; doc.Tables[i + 1].Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing, ref missing); string fileName = @"D:\MyFileTest\MyFileTest\bin\Debug\Template\PICTURE\L-00001.jpg"; replaceKey = "$picture$"; Random rd = new Random(); object linkToFile = false; object saveWithDocument = true; doc.Tables[i + 1].Range.Rows[9].Select(); object range = app.Selection.Range; Microsoft.Office.Interop.Word.InlineShape shape = app.ActiveDocument.InlineShapes.AddPicture(fileName, ref linkToFile, ref saveWithDocument, ref range); shape.Width = 230f;//图片宽度 shape.Height = 230f;//图片高度 shape.ConvertToShape().WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;//四周环绕的方式 if (i + 1 != 2) { object mymissing = System.Reflection.Missing.Value; object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory; app.Selection.EndKey(ref myunit, ref mymissing); object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak; app.Selection.TypeParagraph(); app.Selection.Paste(); } System.Threading.Thread.Sleep(200); } doc.Save(); ret = true; } catch (Exception ex) { ret = false; MessageBox.Show(ex.ToString()); } finally { object o = false; doc.Close(ref o, ref missing, ref missing); app.Quit(ref o, ref missing, ref missing); } return ret; }
Strong08 2017-01-04
  • 打赏
  • 举报
回复
同样问题一直未能解决
wxcyz 2016-04-19
  • 打赏
  • 举报
回复
把插入图片的代码换成下面就好了

object linkToFile = false;
                    object saveWithDocument = true;
                    object range = app.Selection.Range;
                    object left = rd.Next(300, 350);
                    object top = rd.Next(500, 550);
                    float rotation = rd.Next(100, 360);

                    Shape cShape = app.ActiveDocument.Shapes.AddPicture(pngPath, ref linkToFile, ref saveWithDocument, ref left, ref top);
                    cShape.Rotation = rotation;
wxcyz 2016-04-18
  • 打赏
  • 举报
回复
我循环打印的时候第一条打印成功,第二条开始到了那个位置就报错
wxcyz 2016-04-18
  • 打赏
  • 举报
回复
Microsoft.Office.Interop.Word.Shape cShape = doc.Application.ActiveDocument.InlineShapes[1].ConvertToShape(); 这行出错,我这个是一个简单的程序,目的是把一个word模版用相应的内容替换,然后打印,打印的文件有点多,所以要用自动处理。导出的那个可以忽略,是用来调试的,出错的地方是判断有没有那张图片,有就加到模版里面一起打印出来,每个打印的文件最多就一张图片。
为轮子而生 2016-04-18
  • 打赏
  • 举报
回复
①首先你并没有贴出具体是哪一行代码引发了异常,这会让人不知道怎么帮助你。 ②你明明已经定义了名为doc的Document对象,为什么还要用doc.Application.ActiveDocument这样的方式呢? COM组件的异常处理程序通常不会告诉你很详细的信息,所以在调用的时候要很小心,像doc.Application.ActiveDocument.InlineShapes[1].ConvertToShape()这样炒股哦2层甚至已经到达5层的调用,是很危险的,有任何一层对象为null,调用就会出错,打个比方,你怎么知道InlineShapes[1]这句就一定成立呢,万一InlineShapes的个数为0或者1呢? ③你的整个程序对“判断”这个概念几乎不涉及,所以才会发生这样无法解释、超出掌控的异常。 ④最后,也是最重要的,还是要标出究竟哪句出错。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧