Get Word Window Handle?

J

Julian

I have some useful code (incl declarations into user32) for checking
keyboard events but it needs Word's Window Handle (Word 2002) - Excel 2002
has Application.hWnd but there's nothing like that for Word alas.

Anyone know how to do this in VBA? (example would be apreciated)

Thanks

Julian
 
S

Shauna Kelly

Hi Julian

At the top of the module, add the following:
Private Declare Function FindWindowByClass Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName
As Long) As Long


When you want the handle, use

Dim lngHWnd as long

lngHWnd = FindWindowByClass("opusapp", 0&)

FWIW, "Opus" was the code name for the first version of Word for
Windows.


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
J

Julian

Thanks Shauna - just what I was looking for!

I'd found something similar elsewhere but was missing the lpClassName,
which, as you demonstrated, was somewhat arbitrary (not like "XLMAIN")

Haven't tried it yet but it looks like it will do the trick perfectly - then
I will be able to do the auto-saving I want *only* when Word is idle...

VMT! Julian

--
Julian I-Do-Stuff

Some Vista stuff, but mostly just Stuff at http://berossus,blogspot.com
Shauna Kelly said:
Hi Julian

At the top of the module, add the following:
Private Declare Function FindWindowByClass Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As
Long) As Long


When you want the handle, use

Dim lngHWnd as long

lngHWnd = FindWindowByClass("opusapp", 0&)

FWIW, "Opus" was the code name for the first version of Word for Windows.


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
J

Julian

Hi Shauna

That works nicely, but I've just realised I may have been asking the wrong
question (it's certainly harder to debug with this approach!)

What I want to do is to trap keyboard events so that I can note the time of
the last keypress...

any suggestions?

--
Julian I-Do-Stuff

Some Vista stuff, but mostly just Stuff at http://berossus,blogspot.com
Shauna Kelly said:
Hi Julian

At the top of the module, add the following:
Private Declare Function FindWindowByClass Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As
Long) As Long


When you want the handle, use

Dim lngHWnd as long

lngHWnd = FindWindowByClass("opusapp", 0&)

FWIW, "Opus" was the code name for the first version of Word for Windows.


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
J

Jonathan West

Julian said:
Hi Shauna

That works nicely, but I've just realised I may have been asking the wrong
question (it's certainly harder to debug with this approach!)

What I want to do is to trap keyboard events so that I can note the time
of the last keypress...

any suggestions?

It sounds like you are trying to write a keylogger. Many such have already
been written, many of them open-source. Just do a web search for
"keylogger".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top