我有一些代码可以打印docx文档:
Microsoft.Office.Interop.Word.Application wordInstance = new Microsoft.Office.Interop.Word.Application();
FileInfo wordFile = new FileInfo("1.docx");
object fileObject = wordFile.FullName;
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document doc = wordInstance.Documents.Open(ref fileObject);
doc.Activate();
doc.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing);
但我在wordInstance.Documents.Open()上收到异常:
无法将'microsoft.Office.Interop.Word.ApplicationClass'类型的COM对象强制转换为'microsoft.Office.Interop.Word.Application'“
我有Word 2016和Microsoft.Office.Interop.Word版本15.0.0.0。
我该如何解决这个问题?
答案 0 :(得分:0)
我找到了解决方案:
dynamic wordInstance = new Microsoft.Office.Interop.Word.Application();
FileInfo wordFile = new FileInfo("2.docx");
object fileObject = wordFile.FullName;
object oMissing = System.Reflection.Missing.Value;
dynamic doc = wordInstance.Documents.Open(ref fileObject);
doc.Activate();
doc.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
创建WordApplication并打开wordInstance.Documents.Open时,我必须使用动态
感谢MickyD提出了一个好主意!