本文介绍了如何在C#中使用CreateObject打开Word文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过使用以下代码尝试了此操作,但它引发了异常."Exception has been thrown by the target of an invocation."
I tried it by using the following code,but it threw an exception."Exception has been thrown by the target of an invocation."
System.Type wordType = System.Type.GetTypeFromProgID("Word.Application");
Object word = System.Activator.CreateInstance(wordType);
wordType.InvokeMember("Visible", BindingFlags.SetProperty, null, word, new Object[] { true });
Object documents = wordType.InvokeMember("Documents", BindingFlags.GetProperty, null, word, null);
object nullobj = Missing.Value;
string fileName=System.AppDomain.CurrentDomain.BaseDirectory + "assets\\sample_doc.docx";
object[] args = new object[15] { fileName, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj};
Object document = documents.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, documents,args);`
推荐答案
using (FileStream stream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
Document Doc = new Document(stream);
}
这篇关于如何在C#中使用CreateObject打开Word文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!