Forum Statistics
- Forum Members:
- Total Threads:
- Total Posts: 11
There are 1 users currently browsing forums.
|
 |

01-17-2010, 11:34 AM
|
|
Registered User
|
|
Join Date: 03 Oct 2004
Posts: 70
|
|
|
SendKeys
Please Register to Remove these Ads
I want to switch on Caps & Number lock on opening an Excel document. I'm using the following code;
Code:
Sub Auto_Open()
Application.SendKeys ("{CAPSLOCK}{NUMLOCK}")
End Sub
But nothing happens on opening. Where am I going wrong?
|

01-17-2010, 11:42 AM
|
 |
Forums Administrator
|
|
Join Date: 18 Nov 2003
Location: Derbyshire,UK
MS Office Version:Xp; 2007
Posts: 13,769
|
|
|
Re: SendKeys
here's some code
Code:
Private Const VER_PLATFORM_WIN32_NT = 2
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VK_CAPITAL = &H14
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
' API declarations:
Private Declare Function GetVersionEx Lib "kernel32" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long
Public Sub ToggleCapsLock(bTurnOn As Boolean)
'To turn capslock on, set bTurnOn to true
'To turn capslock off, set bTurnOn to false
Dim bytKeys(255) As Byte
Dim bCapsLockOn As Boolean
'Get status of the 256 virtual keys
GetKeyboardState bytKeys(0)
bCapsLockOn = bytKeys(VK_CAPITAL)
Dim typOS As OSVERSIONINFO
If bCapsLockOn <> bTurnOn Then 'if current state <>
'requested state
If typOS.dwPlatformId = _
VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98
bytKeys(VK_CAPITAL) = 1
SetKeyboardState bytKeys(0)
Else '=== WinNT/2000
'Simulate Key Press
keybd_event VK_CAPITAL, &H45, _
KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simulate Key Release
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If
End Sub
Sub switchOn()
ToggleCapsLock (True)
End Sub
Sub switchOff()
ToggleCapsLock (False)
End Sub
__________________
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Scales icon to rate it
For Excel consulting, free examples and tutorials visit my site
Check out the free Excel Toolbar
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Code Tags: Make your code easier for us to read
|

01-17-2010, 06:12 PM
|
 |
Forum Guru
|
|
Join Date: 20 Jun 2007
Location: The Great State of Texas
MS Office Version:2003, 2007
Posts: 18,493
|
|
|
Re: SendKeys
I was pondering why OT's code didn't work; and then it occurred to me that Caps Lock and Num Lock never make it out of the keyboard driver -- Excel wouldn't know what to do with them.
__________________
Entia non sunt multiplicanda sine necessitate.
|

01-17-2010, 07:07 PM
|
 |
Forum Moderator
|
|
Join Date: 15 Jan 2005
Location: San Francisco, Ca
MS Office Version:2000, 2003, & read 2007
Posts: 10,529
|
|
|
Re: SendKeys
Hello OptionTrader,
Here is a more succinct version of the API send keys.
VBA Module Code
Code:
'Written: January 17, 2010
'Author: Leith Ross
'Summary: API routines to set or clear the CAPS, NUM LOCK and SCOLL keys.
Option Explicit
Const NumLock_On = &H20
Const ScrollLock_On = &H40
Const CapsLock_On = &H80
Const vk_Scroll = &H91
Private Declare Sub keybd_event _
Lib "user32.dll" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
Private Declare Function GetKeyState _
Lib "user32.dll" _
(ByVal nVirtKey As Long) As Long
Sub KeyLock(myKey As String, State As Boolean)
'State=True means to press key if state is off
'myKey must be: Num, Scroll, or Caps as String type.
Select Case True
Case myKey Like "Num"
If State <> CBool(GetKeyState(vbKeyNumlock)) Then PressKey (vbKeyNumlock)
Case myKey Like "Scroll"
If State <> CBool(GetKeyState(vk_Scroll)) Then PressKey (vk_Scroll)
Case myKey Like "Caps"
If State <> CBool(GetKeyState(vbKeyCapital)) Then PressKey (vbKeyCapital)
End Select
End Sub
Sub PressKey(theKey As Long)
keybd_event theKey, 0, 0, 0 'press key
keybd_event theKey, 0, &H2, 0 'release key
End Sub
ThisWorkbook_Open Event Code
Code:
Private Sub Workbook_Open()
'Lock these keys
KeyLock "Caps", True
KeyLock "Num Lock", True
End Sub
__________________
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [code] before the first line of code and [/code] after the last line of code.2. Thank those who have helped you by Clicking the scales above each post. 3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Last edited by Leith Ross; 01-17-2010 at 07:10 PM.
|

01-18-2010, 01:20 AM
|
|
Registered User
|
|
Join Date: 03 Oct 2004
Posts: 70
|
|
|
Re: SendKeys
Thanks guys.
shg - Funny thing is original SendKeys code works fine in Excel 2007, but not 2003 (I need it to work in 2003).
Leith - Can't get that code to work.
Roy - Code works fine for Caps lock. How can I incorporate Num lock into the code ?
|

01-18-2010, 01:40 AM
|
 |
Forum Moderator
|
|
Join Date: 15 Jan 2005
Location: San Francisco, Ca
MS Office Version:2000, 2003, & read 2007
Posts: 10,529
|
|
|
Re: SendKeys
Hello OptionTrader,
When dealing with API calls and most other code, it helps to know what Operating System you have. You should update your profile to include which operating systems you use. Roy's checks your operating system, that why his working. So, I can assume you are not using Windows 2000 through Windows XP.
__________________
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [code] before the first line of code and [/code] after the last line of code.2. Thank those who have helped you by Clicking the scales above each post. 3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
|

01-18-2010, 04:27 AM
|
 |
Forums Administrator
|
|
Join Date: 18 Nov 2003
Location: Derbyshire,UK
MS Office Version:Xp; 2007
Posts: 13,769
|
|
|
Re: SendKeys
Just searched through the code on another computer & found this
Code:
Option Explicit
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer
Private Declare Function GetVersionEx Lib "kernel32" _
Alias "GetVersionExA" (lpVersionInformation As _
OSVERSIONINFO) As Long
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long
Private Const VER_PLATFORM_WIN32_NT = 2
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_CAPS = &H14
Private Const VK_NUM = &H90
Private Const VK_SCROLL = &H91
Private keys(0 To 255) As Byte
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Private oSv As OSVERSIONINFO
Private Function KeyStatus(Test) As Boolean
Dim bInsertKeyState As Boolean
oSv.dwOSVersionInfoSize = Len(oSv)
GetVersionEx oSv
GetKeyboardState keys(0)
KeyStatus = keys(Test)
End Function
Private Sub Switch(Test, Switch As Boolean)
If (KeyStatus(Test) <> Switch) Then
If (oSv.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS) Then
keys(Test) = 1
SetKeyboardState keys(0)
ElseIf (oSv.dwPlatformId = VER_PLATFORM_WIN32_NT) Then
keybd_event Test, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
keybd_event Test, &H45, KEYEVENTF_EXTENDEDKEY Or _
KEYEVENTF_KEYUP, 0
End If
End If
End Sub
Public Sub On()
Switch VK_NUM, 1
Switch VK_CAPS, 1
Switch VK_SCROLL, 1
End Sub
Public Sub Off()
Switch VK_NUM, 0
Switch VK_CAPS, 0
Switch VK_SCROLL, 0
End Sub
Public Sub Status()
MsgBox "Num : " & KeyStatus(VK_NUM) & vbNewLine & _
"Caps: " & KeyStatus(VK_CAPS) & vbNewLine & _
"Scroll: " & KeyStatus(VK_SCROLL), vbInformation, "Test status"
End Sub
__________________
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Scales icon to rate it
For Excel consulting, free examples and tutorials visit my site
Check out the free Excel Toolbar
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Code Tags: Make your code easier for us to read
Last edited by royUK; 01-24-2010 at 12:10 PM.
|

01-18-2010, 04:40 AM
|
|
Valued Forum Contributor
|
|
Join Date: 14 May 2009
Location: gold coast
MS Office Version:Excel 2007
Posts: 771
|
|
|
Re: SendKeys
sorry wrong page
__________________
|

01-18-2010, 05:52 AM
|
|
Registered User
|
|
Join Date: 03 Oct 2004
Posts: 70
|
|
|
Re: SendKeys
Quote:
Originally Posted by Leith Ross
Hello OptionTrader,
When dealing with API calls and most other code, it helps to know what Operating System you have. You should update your profile to include which operating systems you use. Roy's checks your operating system, that why his working. So, I can assume you are not using Windows 2000 through Windows XP.
|
Hi Leith
The SendKeys code works fine when using Windows7/Excel2007, but not when using XP/Excel 2003. I need the code in the later combination.
Martin
|

01-24-2010, 11:34 AM
|
|
Registered User
|
|
Join Date: 03 Oct 2004
Posts: 70
|
|
|
Re: SendKeys
Roy, is there any chance you could include the code to also switch on the CapsLock? I'd be most grateful.
Martin
|

01-24-2010, 12:08 PM
|
 |
Forums Administrator
|
|
Join Date: 18 Nov 2003
Location: Derbyshire,UK
MS Office Version:Xp; 2007
Posts: 13,769
|
|
|
Re: SendKeys
Quote:
Originally Posted by OptionTrader
Roy, is there any chance you could include the code to also switch on the CapsLock? I'd be most grateful.
Martin
|
That's in the first code that I posted and in the second
__________________
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Scales icon to rate it
For Excel consulting, free examples and tutorials visit my site
Check out the free Excel Toolbar
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Code Tags: Make your code easier for us to read
|
 |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|