如何在C#中使用CreateObject打开word文档?

如何在C#中使用CreateObject打开word文档?,c#,ms-word,C#,Ms Word,我使用下面的代码进行了尝试,但它引发了一个异常。“异常已由调用的目标引发。” 什么版本的。您好,我已经更新了我的问题,请参考完整的代码。谢谢。我的意思是,我必须使用这样的代码来做。System.Type wordType=System.Type.GetTypeFromProgID(“Word.Application”);Object word=System.Activator.CreateInstance(wordType);抱歉,我还有一个问题。如何获取书签实例,并且我想在某个位置插入书签。无

我使用下面的代码进行了尝试,但它引发了一个异常。
“异常已由调用的目标引发。”


什么版本的。您好,我已经更新了我的问题,请参考完整的代码。谢谢。我的意思是,我必须使用这样的代码来做。System.Type wordType=System.Type.GetTypeFromProgID(“Word.Application”);Object word=System.Activator.CreateInstance(wordType);抱歉,我还有一个问题。如何获取书签实例,并且我想在某个位置插入书签。无论如何,谢谢。
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);
}
public void openWordDocument(string filePath)
{
    object missing = System.Reflection.Missing.Value;

    //create a document in this path  
    object fileName = filePath;

    object wordApp = Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"));
    //Setup our Word.Document  
    object wordDoc = wordApp.GetType().InvokeMember("Documents", System.Reflection.BindingFlags.GetProperty, null, wordApp, null);
    //Set Word to be not visible  
    wordApp.GetType().InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, wordApp, new object[] { true });

    //Open the word document fileName  
    object activeDoc = wordDoc.GetType().InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, wordDoc, new Object[] { fileName });
}