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

CPU and GPU timer in cuda visual profiler

cuda

So there are 2 timers in cuda visual profiler,

Timecop and Ruby 2.0.0

ruby rspec ruby-2.0 timecop

Can anyone suggest why this spec fails using Timecop 0.6.1 and Ruby 2.0.0?

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.