我嘗試了使用下面的代碼,但它引發了一個異常。 "Exception has been thrown by the target of an invocation."
如何在C#中使用CreateObject打開Word文檔?
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);`
什麼是'documents'? – gdoron
什麼版本。你使用的NET。 –
嗨,我已經更新了我的問題,請參閱完整的代碼。謝謝。 – WangHongjian