#1
|
|||
|
|||
Is it possible to bind macros to keys not in the KeyCodeConstants class?
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 |
#2
|
|||
|
|||
As often happens, I find that asking the question helps me formulate what my question actually is, and I've come up with a solution.
I don't need to bind the macro to the punctuation, I just need to check to see whether the selection is punctuation and if so, make a new selection to the old selection's left. Here's the code: Code:
Sub CheckLastWord2() 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 'if the selection is 2 characters, assume it is punctuation and move to the left of it If Selection.Range.Characters.Count = 2 Then Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend End If ' 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 'if there are no errors, move the cursor back where it was Else: Selection.MoveRight Unit:=wdCharacter, Count:=2 End If End Sub |
#3
|
|||
|
|||
question mark is a reserved character and can not be used for key-binding
|
#4
|
|||
|
|||
I thought that question mark may be a reserved character, but it is not. Have you tried to set via the GUI, rather than by code
|
#5
|
|||
|
|||
I have not, but I'm honestly not sure what this means... I've read that there are ways to do this outside of Word, through Windows instead, but I'm afraid of causing problems with Windows that may spread to other programs. Do you think this is worth the risk?
|
Tags |
key bindings, keycodeconstants |
Thread Tools | |
Display Modes | |
|
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 |