C# 保存以编程方式创建的包含图像的文档时出错

C# 保存以编程方式创建的包含图像的文档时出错,c#,ms-word,vsto,C#,Ms Word,Vsto,我有一个C#函数,它使用VSTO将提供的图像保存到PDF文件中。但它是: System.AccessViolationException HResult=0x80004003 消息=尝试读取或写入受保护内存。这通常是一个问题 表示其他内存已损坏。源=堆栈跟踪: 我尝试过以各种方式保存:请参见注释代码: 公共静态布尔ConvertImageFileToPDF(字符串imageFileName、字符串outputFileName、字符串attachmentName) { 尝试 { Microsoft

我有一个C#函数,它使用VSTO将提供的图像保存到PDF文件中。但它是:

System.AccessViolationException HResult=0x80004003
消息=尝试读取或写入受保护内存。这通常是一个问题 表示其他内存已损坏。源=堆栈跟踪:

我尝试过以各种方式保存:请参见注释代码:

公共静态布尔ConvertImageFileToPDF(字符串imageFileName、字符串outputFileName、字符串attachmentName)
{
尝试
{
Microsoft.Office.Interop.Word.Application wordApp=新的Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc;
wordDoc=wordApp.Documents.Add();
wordDoc.Content.Font.Name=“Arial”;
wordDoc.Content.Font.Size=12;
wordDoc.Content.ParagraphFormat.Alignment=Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
wordDoc.Content.Text=附件名称;
wordDoc.Content.InsertParagraphAfter();
AddPicture(imageFileName,false,true);
wordDoc.Application.ActiveDocument.SaveAs2(输出文件名,WdSaveFormat.wdFormatPDF);
//SaveAs2(输出文件名,WdSaveFormat.wdFormatPDF);
//SaveAs(outputFileName,WdSaveFormat.wdFormatPDF);
wordDoc.Close();
wordApp.Application.Quit(false);
返回true;
}
抓住
{
返回false;
}
保存位置是有效的(代码中其他地方使用的简单临时目录,没有问题)


我一直在寻找解决方案,但运气不佳,有人能帮我吗?

如果关闭Windows,重新启动并再次运行代码,是否有效?第二次(和以后的时间)无效?如果关闭Windows,重新启动并再次运行代码,是否有效?第二次(和以后的时间)无效?