![]() |
|
#1
|
|||
|
|||
|
Hello all,
I'm trying to bind a macro to certain keys in Word, and I'm having trouble with keys that aren't in the KeyCodeConstants class. For starters, I had trouble even finding out what the codes were, but I created a textbox in a userform and used the following code to get 63 for the "?" key: Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) MsgBox KeyAscii End Sub Code:
private sub yes
msgbox "yes"
end sub
Sub SetBindings()
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(32), KeyCategory:=wdKeyCategoryMacro, Command:="yes"
KeyBindings.Add KeyCode:=BuildKeyCode(63), KeyCategory:=wdKeyCategoryMacro, Command:="yes"
end sub
Within the context of my project, this would mean that I can only trigger my macro with the spacebar. The problem is that I need to check the word before the spacebar for errors against Word's internal spell check collection. This works fine when there's no punctuation, but in the case of any punctuation, the macro proceeds as though the word was spelled correctly. Here's the code for the actual macro (modified from code I found here: http://www.vbaexpress.com/kb/getarticle.php?kb_id=204) Code:
Sub CheckLastWord()
Dim errorCount As Object
' Insert a space since this macro bound to the spacebar
Selection.TypeText Text:=" "
' Select word just typed including space with cursor to left of word
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
' Set errorCount to count errors in selected text
Set errorCount = Selection.Range.SpellingErrors
' move cursor to the right of word just typed
Selection.MoveRight Unit:=wdCharacter, Count:=1
If errorCount.Count > 0 Then
MsgBox "Wrong"
' select the word just typed ready for retyping
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
End If
End Sub
|
| Tags |
| key bindings, keycodeconstants |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Evernote--Class Notes | markg2 | Outlook | 0 | 05-10-2012 05:50 PM |
| C, V, and H keys not working in new message | jtpryan | Outlook | 0 | 06-30-2011 03:22 AM |
| Looking for easy and quick way to draw on class notes "x-rays - image" | Lacrosseboss18 | PowerPoint | 0 | 02-04-2011 01:21 PM |
| Can't Assign Shortcut Keys | Vicki F. | Word | 0 | 04-17-2010 03:03 PM |
| Want to search for "class", replacing with document property | YetAnotherAuthor | Word | 0 | 10-30-2009 09:43 AM |