Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 104
Default SENDKEY method

I'VE NEVER USED THE SENDKEYS METHOD BEFORE AND I'M HAVING A BIT OF A
PROBLEM.

All I want is for the computer to put the CapsLock on when a certain
workbook is activated, and then to turn off the CapsLock when the book
is deactivated; why doesn't the below work?

Private Sub Workbook_Activate()
Application.SendKeys "{CAPSLOCK}", True
End Sub

Private Sub Workbook_Deactivate()
Application.SendKeys "%{CAPSLOCK}", True
End Sub

Any help greatly appreciated

Jason
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default SENDKEY method

Don't use Sendkeys if you can avoid it. So your first question should be
how to turn Caps lock on/off programmatically. See if this works:

Declare Sub keybd_event Lib "USER32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal
dwExtraInfo As Long)

Const KEYEVENTF_KEYUP = &H2
Const KEYEVENTF_EXTENDEDKEY = &H1

Sub NTKeyCapslockOn()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 0 Then
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key
down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End If
End Sub

Sub NTKeyCapslockOff()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 1 Then
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key
down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End If
End Sub


--
Jim Rech
Excel MVP
"jason" wrote in message
om...
| I'VE NEVER USED THE SENDKEYS METHOD BEFORE AND I'M HAVING A BIT OF A
| PROBLEM.
|
| All I want is for the computer to put the CapsLock on when a certain
| workbook is activated, and then to turn off the CapsLock when the book
| is deactivated; why doesn't the below work?
|
| Private Sub Workbook_Activate()
| Application.SendKeys "{CAPSLOCK}", True
| End Sub
|
| Private Sub Workbook_Deactivate()
| Application.SendKeys "%{CAPSLOCK}", True
| End Sub
|
| Any help greatly appreciated
|
| Jason


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 104
Default SENDKEY method

Hi Jim,

Thanks for the help.
What is the function GetKeyboardState. This doesn't seem to get
recognised. Do I need to add a reference so that it gets recognised or
is it another API function that I'm missing?

Could I just use the below? Suppose it won't know what state the users
capslock is already in!

Sub NTKeyCapslockToggle()
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0
''key down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End Sub

Help greatly appreciated
Jason



"Jim Rech" wrote in message ...
Don't use Sendkeys if you can avoid it. So your first question should be
how to turn Caps lock on/off programmatically. See if this works:

Declare Sub keybd_event Lib "USER32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal
dwExtraInfo As Long)

Const KEYEVENTF_KEYUP = &H2
Const KEYEVENTF_EXTENDEDKEY = &H1

Sub NTKeyCapslockOn()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 0 Then
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key
down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End If
End Sub

Sub NTKeyCapslockOff()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 1 Then
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key
down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End If
End Sub


--
Jim Rech
Excel MVP
"jason" wrote in message
om...
| I'VE NEVER USED THE SENDKEYS METHOD BEFORE AND I'M HAVING A BIT OF A
| PROBLEM.
|
| All I want is for the computer to put the CapsLock on when a certain
| workbook is activated, and then to turn off the CapsLock when the book
| is deactivated; why doesn't the below work?
|
| Private Sub Workbook_Activate()
| Application.SendKeys "{CAPSLOCK}", True
| End Sub
|
| Private Sub Workbook_Deactivate()
| Application.SendKeys "%{CAPSLOCK}", True
| End Sub
|
| Any help greatly appreciated
|
| Jason

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default SENDKEY method

Here is the missing declaration. If you want to have it on for a certain
workbook, it doesn't seem like toggling would be the best approach. You
would turn it On when teh workbook is activated and turn it off when
deactivated:

Declare Sub GetKeyboardState Lib "user32" _
(pbKeyState As Byte)

Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal _
dwExtraInfo As Long)

Const KEYEVENTF_KEYUP = &H2
Const KEYEVENTF_EXTENDEDKEY = &H1

Sub NTKeyCapslockOn()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 0 Then
keybd_event vbKeyCapital, 0, _
KEYEVENTF_EXTENDEDKEY Or 0, 0 ''keydown
keybd_event vbKeyCapital, 0, _
KEYEVENTF_EXTENDEDKEY Or _
KEYEVENTF_KEYUP, 0
End If
End Sub

Sub NTKeyCapslockOff()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 1 Then
keybd_event vbKeyCapital, 0, _
KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key down
keybd_event vbKeyCapital, 0, _
KEYEVENTF_EXTENDEDKEY Or _
KEYEVENTF_KEYUP, 0
End If
End Sub


--
Regards,
Tom Ogilvy

"jason" wrote in message
m...
Hi Jim,

Thanks for the help.
What is the function GetKeyboardState. This doesn't seem to get
recognised. Do I need to add a reference so that it gets recognised or
is it another API function that I'm missing?

Could I just use the below? Suppose it won't know what state the users
capslock is already in!

Sub NTKeyCapslockToggle()
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0
''key down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End Sub

Help greatly appreciated
Jason



"Jim Rech" wrote in message

...
Don't use Sendkeys if you can avoid it. So your first question should

be
how to turn Caps lock on/off programmatically. See if this works:

Declare Sub keybd_event Lib "USER32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal
dwExtraInfo As Long)

Const KEYEVENTF_KEYUP = &H2
Const KEYEVENTF_EXTENDEDKEY = &H1

Sub NTKeyCapslockOn()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 0 Then
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key
down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End If
End Sub

Sub NTKeyCapslockOff()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 1 Then
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key
down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End If
End Sub


--
Jim Rech
Excel MVP
"jason" wrote in message
om...
| I'VE NEVER USED THE SENDKEYS METHOD BEFORE AND I'M HAVING A BIT OF A
| PROBLEM.
|
| All I want is for the computer to put the CapsLock on when a certain
| workbook is activated, and then to turn off the CapsLock when the book
| is deactivated; why doesn't the below work?
|
| Private Sub Workbook_Activate()
| Application.SendKeys "{CAPSLOCK}", True
| End Sub
|
| Private Sub Workbook_Deactivate()
| Application.SendKeys "%{CAPSLOCK}", True
| End Sub
|
| Any help greatly appreciated
|
| Jason



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 104
Default SENDKEY method

Cheers Tom: Now works fine
Looks pretty useful as I seem to be able to replace vbKeyCapital with
other constants, cven though I'm not too sure how these APIs work !

Jason

"Tom Ogilvy" wrote in message ...
Here is the missing declaration. If you want to have it on for a certain
workbook, it doesn't seem like toggling would be the best approach. You
would turn it On when teh workbook is activated and turn it off when
deactivated:

Declare Sub GetKeyboardState Lib "user32" _
(pbKeyState As Byte)

Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal _
dwExtraInfo As Long)

Const KEYEVENTF_KEYUP = &H2
Const KEYEVENTF_EXTENDEDKEY = &H1

Sub NTKeyCapslockOn()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 0 Then
keybd_event vbKeyCapital, 0, _
KEYEVENTF_EXTENDEDKEY Or 0, 0 ''keydown
keybd_event vbKeyCapital, 0, _
KEYEVENTF_EXTENDEDKEY Or _
KEYEVENTF_KEYUP, 0
End If
End Sub

Sub NTKeyCapslockOff()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 1 Then
keybd_event vbKeyCapital, 0, _
KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key down
keybd_event vbKeyCapital, 0, _
KEYEVENTF_EXTENDEDKEY Or _
KEYEVENTF_KEYUP, 0
End If
End Sub


--
Regards,
Tom Ogilvy

"jason" wrote in message
m...
Hi Jim,

Thanks for the help.
What is the function GetKeyboardState. This doesn't seem to get
recognised. Do I need to add a reference so that it gets recognised or
is it another API function that I'm missing?

Could I just use the below? Suppose it won't know what state the users
capslock is already in!

Sub NTKeyCapslockToggle()
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0
''key down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End Sub

Help greatly appreciated
Jason



"Jim Rech" wrote in message

...
Don't use Sendkeys if you can avoid it. So your first question should

be
how to turn Caps lock on/off programmatically. See if this works:

Declare Sub keybd_event Lib "USER32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal
dwExtraInfo As Long)

Const KEYEVENTF_KEYUP = &H2
Const KEYEVENTF_EXTENDEDKEY = &H1

Sub NTKeyCapslockOn()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 0 Then
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key
down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End If
End Sub

Sub NTKeyCapslockOff()
Dim lpbKeyState(0 To 255) As Byte
GetKeyboardState lpbKeyState(0)
If lpbKeyState(vbKeyCapital) = 1 Then
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or 0, 0 ''key
down
keybd_event vbKeyCapital, 0, KEYEVENTF_EXTENDEDKEY Or
KEYEVENTF_KEYUP, 0
End If
End Sub


--
Jim Rech
Excel MVP
"jason" wrote in message
om...
| I'VE NEVER USED THE SENDKEYS METHOD BEFORE AND I'M HAVING A BIT OF A
| PROBLEM.
|
| All I want is for the computer to put the CapsLock on when a certain
| workbook is activated, and then to turn off the CapsLock when the book
| is deactivated; why doesn't the below work?
|
| Private Sub Workbook_Activate()
| Application.SendKeys "{CAPSLOCK}", True
| End Sub
|
| Private Sub Workbook_Deactivate()
| Application.SendKeys "%{CAPSLOCK}", True
| End Sub
|
| Any help greatly appreciated
|
| Jason

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please post this thread a complete correct method, method about te Nast Runsome New Users to Excel 0 February 23rd 08 09:42 PM
sendkey help needed Ryk Excel Discussion (Misc queries) 4 November 6th 06 04:50 AM
sendkey problem Anders Andersson[_2_] Excel Programming 5 September 29th 04 05:53 PM
SendKey Question Matt Excel Programming 0 August 9th 04 07:59 PM
using sendkey to enter vbe password in Excel 2000 jaf Excel Programming 0 September 30th 03 05:53 PM


All times are GMT +1. The time now is 01:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"