Is it possible to handle the events for MSWord ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi
is there is anyway to capture the document events of a MSWord?eg:I have to
handle the event awhen WM_KEYUP or anyother events will occur.
give me a solution............
thanks in advance
lkr
 
If you want to consume MS Word events within an existing VS.NET C# Project use Automation:

Add a reference to the Word interop assembly in your Project. If it is not available in the .NET tab of the Add Reference dialog,
then click "Browse..." and locate the MS Word executable and add a reference to it instead.

Research if there is a Word Primary Interop Assembly, which should be referenced if avialable instead of the above solution. I'm
not sure if it's available, or for what versions of Word, at this time.

VS.NET will wrap it as a .NET assembly unless you choose one from the .NET tab, or reference a PIA (Primary Interop Assembly)
provided by MS. Once referenced, you can code against MS Word as you would any other .NET assembly, subscribing to events, etc.

To get an instance Word (running or new):

Declare a method:

/// <summary>Retrieves a reference to a running COM object with the specified ProgID or creates a new one.</summary>
public static object GetRunningObject(string ProgID)
{
try
{
// First, check the running object table (ROT)
return Marshal.GetActiveObject(ProgID);
}
catch {}

object instance = null;

try
{
// Create a new instance
Type type = Type.GetTypeFromProgID(ProgID, false);
instance = Activator.CreateInstance(type);
}
catch {}

return instance;
}


Invoke the method:

Word._Application word = GetRunningObject("Word.Application") as Word._Application

Subscribe to an event:

word.Application.DocumentOpen+=
new Word.ApplicationEvents2_DocumentOpenEventHandler(Application_DocumentOpen);
 
Hi,

Actually there is a simpler way , just call the Win32API FindWindow and get
the handle of your word app (you can use Spy++ to get the name) and then you
can PeekMessages of that handle, you can trap WM_KEYUP or any other message
and perform your actions. Remember, everything in windows are windows :)
including the Start button, give it a try .

Cheers
Salva
 
hi Salva
Thanks for u reply.........
By using the the Win32API FindWindow i got the handle to MSWord ,But It is
the main window handle ,Again i tried to get the handle of child window ,i
used EnumChildWindows Win32API i got the hadle of ScrollBar window.i checked
it wit sp++ FinWindow.Handle returned r same............
But my problem is that i was not able to subclass this scrollbar.............
Is it possible ?
 
Using FindWindow won't necessarily work with all versions of Word since your using Spy++ to obtain a window name for a particular
version. Using automation will be more robust in that Microsoft can now change the names of their windows, even remove them
completely, and your event hookups will remain in-tact given that they do not remove the events in successive versions.
 
Hi Dave
Thanks for ur reply...............
I already done automation of MSWord.But i want to handle the document
window and ScrollBar window seperatly,There is no solution for this one in
automation.
Is there is any way for this, plse help me.......?

thanks in advance
lkr
 
If Word doesn't supply the events you need, then you'll have to use a "hack" such as FindWindowEx, but use it with caution.
 
Hi Dave
i also tried to get the event by using FindWindowEx(),But i didnt get the
handle to the childwindow using FindWindowEx ().But i got child window handle
by EnumChildWindow but i was not able to subclass and handle the
events.........
How can i subclass this child window?
lkr
 
Back
Top