Search code examples
.netautomationms-officeoffice-2007

NET code to "listen" to an office application (get the events raised by the user)


I am on the verge of remaking an application for e-learning. But now I want to take this new version to a new level.

So this the scenario: a user opens up my app. He/she then chooses to be trained in one of the most common MS Office applications (Word,Excel,Outlook, etc).

My app will give the user instructions on what to do: create a new document, type some text, turn it bold, insert a picture, etc. If done correctly the user will be awarded points. If not, demerits.

But (there is always a but )for me to accomplish this I have to be able to "see" what the user is doing. I am using NET (2.0 - C# or VB, doesn't matter which). Can anyone tell how to do this? I need to get a handle to the open Office app or start a new one and then I have to listen and evaluate everything the user does.If the user does it wrong I also need to send instructions to the Office app and do it for him/her.

Is this possible? Does anyone has some samples of code on how to do this or anything similar?

Thanks in advance

PS:I really wouldn't want to have a Office add on to accomplish this!

Edited: The reason I don't want to get into VBA is that I wanted to make sure that this was reusable to assist users in using other applications further down the road. So I wanted some way to create a listener to an application and decode the user interaction with it.


Solution

  • There is actually a Windows hook designed for computer based training applications called WH_CBT and it provides notification when windows are activated, minimized, moved, resized, commands are executed, mouse/keyboard events, etc.

    I won't lie to you though, hooking is a fairly complex topic but is certainly approachable from .NET. Have a look at the following MSDN article that explains how to use message hooking in .NET and then have a look at the two links that follow for more information about WH_CBT.

    Cutting Edge: Windows Hooks in the .NET Framework

    MSDN Library: SetWindowsHookEx Function

    MSDN Library: CBTProc Function