Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2013, 12:51 PM
AlexR AlexR is offline Is it possible to bind macros to keys not in the KeyCodeConstants class? Windows XP Is it possible to bind macros to keys not in the KeyCodeConstants class? Office 2007
Novice
Is it possible to bind macros to keys not in the KeyCodeConstants class?
 
Join Date: Mar 2013
Location: Eastern PA, USA
Posts: 8
AlexR is on a distinguished road
Default 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
Next I attempted to bind the space bar (as an experimental control) and the "?" with the following code:
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
The end result is that the spacebar triggers the "yes" msgbox but the "?" does not. I conclude from this that either the "?" cannot be bound to a macro or I did something screwy. Can anyone confirm?

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
I could use some suggestions as to how to get around the punctuation issue. Perhaps I could use an If statement to skip over the punctuation mark, but I'm not sure how that would work. Obviously I'm not a programmer, and I'm just hacking my way through this. I appreciate the help!
Reply With Quote
  #2  
Old 03-31-2013, 01:51 PM
AlexR AlexR is offline Is it possible to bind macros to keys not in the KeyCodeConstants class? Windows XP Is it possible to bind macros to keys not in the KeyCodeConstants class? Office 2007
Novice
Is it possible to bind macros to keys not in the KeyCodeConstants class?
 
Join Date: Mar 2013
Location: Eastern PA, USA
Posts: 8
AlexR is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 04-03-2013, 07:57 PM
fumei fumei is offline Is it possible to bind macros to keys not in the KeyCodeConstants class? Windows 7 64bit Is it possible to bind macros to keys not in the KeyCodeConstants class? Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

question mark is a reserved character and can not be used for key-binding
Reply With Quote
  #4  
Old 04-03-2013, 08:06 PM
fumei fumei is offline Is it possible to bind macros to keys not in the KeyCodeConstants class? Windows 7 64bit Is it possible to bind macros to keys not in the KeyCodeConstants class? Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

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
Reply With Quote
  #5  
Old 04-08-2013, 10:15 AM
AlexR AlexR is offline Is it possible to bind macros to keys not in the KeyCodeConstants class? Windows XP Is it possible to bind macros to keys not in the KeyCodeConstants class? Office 2007
Novice
Is it possible to bind macros to keys not in the KeyCodeConstants class?
 
Join Date: Mar 2013
Location: Eastern PA, USA
Posts: 8
AlexR is on a distinguished road
Default

Quote:
Originally Posted by fumei View Post
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
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?
Reply With Quote
Reply

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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:18 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2021 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft