Hi Hans,
...continuing building on my 'StyleBuilder', where I'm trying to extracta large set of information as 'style documentation'.
In the attached document, I'm trying to get out any shortcut keys associated with specific styles.
However, when I check the KeyBindings collection nothing seems to be registered, regardless of whether the shortcuts are being added to the styles manually or programmatically.
In the attached document, I've added a bit of both, but regardless - when the macro tries to do a count the collection is always empty.
Any idea what might be going wrong here??
Shortcut keys (Keybindings)
-
- Lounger
- Posts: 34
- Joined: 28 Oct 2011, 09:05
Shortcut keys (Keybindings)
You do not have the required permissions to view the files attached to this post.
Thanks and best regards
RD
A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.
RD
A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.
-
- Administrator
- Posts: 78507
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Shortcut keys (Keybindings)
Insert the following line at the beginning of the macro:
Otherwise, you're looking at the KeyBindings of the Normal template.
Code: Select all
Application.CustomizationContext = ThisDocument
Best wishes,
Hans
Hans
-
- Lounger
- Posts: 34
- Joined: 28 Oct 2011, 09:05
Re: Shortcut keys (Keybindings)
That may be true for the macro, but when I added the short-cuts manually, I made certain that they were registered on the document and NOT the normal.dot.
Is it so that the Keybindings collection by default always looks at the normal.dot unless otherwise stated as you mention in your code...???
Is it so that the Keybindings collection by default always looks at the normal.dot unless otherwise stated as you mention in your code...???
Thanks and best regards
RD
A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.
RD
A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.
-
- Administrator
- Posts: 78507
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Shortcut keys (Keybindings)
Yes, the default for CustomizationContext is NormalTemplate. So if you add a KeyBinding using code without setting CustomizationContext to something else, you will create the KeyBinding in the default template, and if you inspect KeyBindings without setting CustomizationContext to something explicitly, you'll see the KeyBindings in the default template.
Best wishes,
Hans
Hans
-
- Lounger
- Posts: 34
- Joined: 28 Oct 2011, 09:05
Re: Shortcut keys (Keybindings)
Thanks Hans,
For the keybindings (kb) I would like to,
1. retrieve any currently set kb in the doc and it's attached template
2. add a shortcut for Headings 1-9 being ALT+1-9 for the doc (if no such kb is already set)
Problem seems to be that the 'BuildKeyCode' takes Word constants as arguments, eg.
KeyBindings.Add KeyCategory:=wdKeyCategoryStyle, Command:=.NameLocal, KeyCode:=BuildKeyCode(wdKey1, wdKeyAlt)
...my hope was that I could set the wdKey* argument as a variable, but since it's a word constant, that doesn't really seem to work (the constants translates into values of 49-57).
Do you have any good ideas on getting around this??
I was down this path a couple of times in various ways, but got stock on transforming the wdkey...
For i = 1 To ActiveDocument.Styles.Count
Set oStyle = ActiveDocument.Styles(i)
With oStyle
'Debug.Print .NameLocal
If InStr(1, .NameLocal, "Heading", vbTextCompare) > 0 Then
On Error Resume Next
intHnum = CInt(Right(.NameLocal, 1))
If 0 < intHnum And intHnum < 10 Then
strKey = "WdKey" & CStr(intHnum)
Application.CustomizationContext = ThisDocument
KeyBindings.Add KeyCategory:=wdKeyCategoryStyle, Command:=.NameLocal, KeyCode:=BuildKeyCode(lngWdKey, wdKeyAlt)
'KeyBindings.Add KeyCategory:=wdKeyCategoryStyle, Command:=.NameLocal, KeyCode:=BuildKeyCode(wdKey1, wdKeyAlt)
End If
On Error GoTo 0
End If
End With
Next i
For the keybindings (kb) I would like to,
1. retrieve any currently set kb in the doc and it's attached template
2. add a shortcut for Headings 1-9 being ALT+1-9 for the doc (if no such kb is already set)
Problem seems to be that the 'BuildKeyCode' takes Word constants as arguments, eg.
KeyBindings.Add KeyCategory:=wdKeyCategoryStyle, Command:=.NameLocal, KeyCode:=BuildKeyCode(wdKey1, wdKeyAlt)
...my hope was that I could set the wdKey* argument as a variable, but since it's a word constant, that doesn't really seem to work (the constants translates into values of 49-57).
Do you have any good ideas on getting around this??
I was down this path a couple of times in various ways, but got stock on transforming the wdkey...
For i = 1 To ActiveDocument.Styles.Count
Set oStyle = ActiveDocument.Styles(i)
With oStyle
'Debug.Print .NameLocal
If InStr(1, .NameLocal, "Heading", vbTextCompare) > 0 Then
On Error Resume Next
intHnum = CInt(Right(.NameLocal, 1))
If 0 < intHnum And intHnum < 10 Then
strKey = "WdKey" & CStr(intHnum)
Application.CustomizationContext = ThisDocument
KeyBindings.Add KeyCategory:=wdKeyCategoryStyle, Command:=.NameLocal, KeyCode:=BuildKeyCode(lngWdKey, wdKeyAlt)
'KeyBindings.Add KeyCategory:=wdKeyCategoryStyle, Command:=.NameLocal, KeyCode:=BuildKeyCode(wdKey1, wdKeyAlt)
End If
On Error GoTo 0
End If
End With
Next i
Thanks and best regards
RD
A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.
RD
A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty.
If it is important to you, you'll find a way. If not you'll find an excuse.
-
- Administrator
- Posts: 78507
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Shortcut keys (Keybindings)
It's a lot simpler than that:
Note that you have to set CustomizationContext only once.
Code: Select all
Sub RubberDuck()
Dim i As Long
Dim oStyle As Style
Dim wdMyKey As WdKey
Application.CustomizationContext = ThisDocument
For i = 1 To 9
' wdStyleHeading1 = -2, wdStyleHeading2 = -3, etc.
Set oStyle = ActiveDocument.Styles(-1 - i)
wdMyKey = wdKey0 + i
KeyBindings.Add _
KeyCategory:=wdKeyCategoryStyle, _
Command:=oStyle.NameLocal, _
KeyCode:=BuildKeyCode(wdMyKey, wdKeyAlt)
Next i
End Sub
Best wishes,
Hans
Hans
-
- Microsoft MVP
- Posts: 1318
- Joined: 24 May 2013, 15:33
- Location: Warminster, PA
Re: Shortcut keys (Keybindings)
A word of caution here: In a running macro, the variable ThisDocument refers to the document or template that contains the running code, which is not necessarily the file that contains the keybindings you're looking for.
If the keybindings are in the ActiveDocument (or in a template on which the ActiveDocument is based) but the macro code is somewhere else (say, in Normal.dotm or another template), then you must set the CustomizationContext to ActiveDocument or to ActiveDocument.AttachedTemplate, not to ThisDocument.
If the keybindings are in the ActiveDocument (or in a template on which the ActiveDocument is based) but the macro code is somewhere else (say, in Normal.dotm or another template), then you must set the CustomizationContext to ActiveDocument or to ActiveDocument.AttachedTemplate, not to ThisDocument.
-
- Administrator
- Posts: 78507
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands