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().InvokeMember("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?

Answers


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.


Need Your Help

Excel VBA: A VBA Regular Expression to check the name of a file?

regex excel vba excel-vba

So basically I writing all of these VBA macros to massage a lot of data. Before that the user should be able to input a date and retrieve the data that they want massaged. The data that needs to be

Payment method sort order. Won't work

magento checkout magento-1.5

I'm trying to change my payment method sort order but it won't work.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.