I have an add-in template I created for MSWord (basically customized search and replace code). It runs from a dot/dotm template file in the MSWord startup folder. Code works just fine.
My problem is with trying to assign a shortcut key to run the "starting" procedure (SRStagsMain).
Im looking for a VBA code solution, not the "macro wizard" or steps on how to customize it through the TOOLS menu.
I have tried using a few examples off the internet, but I still can't get the shortcut key to work in MSWord 2007 (have not tried the binding on 2003)
I have tried using the following code to bind the shortcut key:
Const myProcName = "SRStags.SRStagMain"
Dim myKeyBinding As KeyBinding
For Each myKeyBinding In KeyBindings
If myKeyBinding.Command = myProcName Then
myKeyBinding.Clear
End If
Next myKeyBinding
KeyBindings.Add KeyCategory:=wdKeyCategoryCommand, _
Command:=myProcName, _
KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyX)
I have also tried replacing the KeyCategory argument to
KeyCategory:=wdKeyCategoryMacro
and still no success. Also tried it without using the myKeyBinding.Clear part of the code.
When the code executes the KeyBindings.Add, I receive the following error message: Error Number=5346 "Word cannot change the function of the specified key."
Can someone point me in the right direction?
Below is the code for my AutoExec and SRStagsMain procedures, in case something is not set up in there correctly.
Public Sub AutoExec()
On Error GoTo ERR_HANDLE
Dim oCmd As CommandBarButton
Dim oCtrl As CommandBarControl
CustomizationContext = NormalTemplate
KeyBindings.Add wdKeyCategoryCommand, "SRStagMain", _
BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyX)
For Each oCtrl In CommandBars("Tools").Controls
If oCtrl.Caption = "SRStags" Then
oCtrl.Delete
End If
Next oCtrl
Set oCmd = CommandBars("Tools").Controls.Add(Type:=msoControlButton, Before:=1)
oCmd.TooltipText = "Search and Replace Symbol tags <SHIFT><ALT><S>"
oCmd.Caption = "SRStags"
oCmd.OnAction = "SRStagsMain"
Set oCmd = Nothing
Set oCtrl = Nothing
EXIT_PROCEDURE:
On Error GoTo 0
Exit Sub
ERR_HANDLE:
Select Case Err.Number
Case 5346
On Error GoTo 0
Resume Next
Case Else
MsgBox "An ERROR has occurred in Sub AutoExec() of Module " & _
"modSRStags!" & vbCrLf & vbCrLf & "(" & Err.Number & ") " & _
Err.Description, vbExclamation + vbMsgBoxSetForeground, _
"WARNING! RUNTIME ERROR (" & Err.Number & ")!"
Resume EXIT_PROCEDURE
End Select
End Sub
The function GetSRStagsFolder looks up the working folder path from a text file
Private Sub SRStagsMain()
FiDO GetSRStagsFolder 'Fetch!
End Sub 'SRStagsMain
My problem is with trying to assign a shortcut key to run the "starting" procedure (SRStagsMain).
Im looking for a VBA code solution, not the "macro wizard" or steps on how to customize it through the TOOLS menu.
I have tried using a few examples off the internet, but I still can't get the shortcut key to work in MSWord 2007 (have not tried the binding on 2003)
I have tried using the following code to bind the shortcut key:
Const myProcName = "SRStags.SRStagMain"
Dim myKeyBinding As KeyBinding
For Each myKeyBinding In KeyBindings
If myKeyBinding.Command = myProcName Then
myKeyBinding.Clear
End If
Next myKeyBinding
KeyBindings.Add KeyCategory:=wdKeyCategoryCommand, _
Command:=myProcName, _
KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyX)
I have also tried replacing the KeyCategory argument to
KeyCategory:=wdKeyCategoryMacro
and still no success. Also tried it without using the myKeyBinding.Clear part of the code.
When the code executes the KeyBindings.Add, I receive the following error message: Error Number=5346 "Word cannot change the function of the specified key."
Can someone point me in the right direction?
Below is the code for my AutoExec and SRStagsMain procedures, in case something is not set up in there correctly.
Public Sub AutoExec()
On Error GoTo ERR_HANDLE
Dim oCmd As CommandBarButton
Dim oCtrl As CommandBarControl
CustomizationContext = NormalTemplate
KeyBindings.Add wdKeyCategoryCommand, "SRStagMain", _
BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyX)
For Each oCtrl In CommandBars("Tools").Controls
If oCtrl.Caption = "SRStags" Then
oCtrl.Delete
End If
Next oCtrl
Set oCmd = CommandBars("Tools").Controls.Add(Type:=msoControlButton, Before:=1)
oCmd.TooltipText = "Search and Replace Symbol tags <SHIFT><ALT><S>"
oCmd.Caption = "SRStags"
oCmd.OnAction = "SRStagsMain"
Set oCmd = Nothing
Set oCtrl = Nothing
EXIT_PROCEDURE:
On Error GoTo 0
Exit Sub
ERR_HANDLE:
Select Case Err.Number
Case 5346
On Error GoTo 0
Resume Next
Case Else
MsgBox "An ERROR has occurred in Sub AutoExec() of Module " & _
"modSRStags!" & vbCrLf & vbCrLf & "(" & Err.Number & ") " & _
Err.Description, vbExclamation + vbMsgBoxSetForeground, _
"WARNING! RUNTIME ERROR (" & Err.Number & ")!"
Resume EXIT_PROCEDURE
End Select
End Sub
The function GetSRStagsFolder looks up the working folder path from a text file
Private Sub SRStagsMain()
FiDO GetSRStagsFolder 'Fetch!
End Sub 'SRStagsMain