开发者

Using Late-Binding to Automate Word is throwing a MissingMemberException

i am trying to access some information from a running Microsoft Word application using the following code..

object appClass = Marshal.GetActiveObject("Word.Application");
object documents = appClass.GetType().GetProperty("Documents");
object count = documents.GetType().InvokeMem开发者_如何学编程ber("Count", BindingFlags.GetProperty, null, documents, null);

When i run this code it tells me that that Count was not found and has thrown a MissingMemberException.

Can anyone tell me what i am doing wrong?


You didn't get a reference to the Documents object, GetProperty returns a PropertyInfo. Fix:

        object appClass = Marshal.GetActiveObject("Word.Application");
        object documents = appClass.GetType().InvokeMember("Documents", BindingFlags.GetProperty, null, appClass, null);
        object count = documents.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, documents, null);

Adding a reference to Microsoft.Office.Word.Interop can make this a lot less painful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜