none
shortcut madness... RRS feed

  • Question

  • Let me start by saying that I'm a .NET dev, not an Office dev, so I'm a wee bit out of my depth here....

    I built a template for a friend that does some calculations in a table. I assigned the primary processing macro to a shortcut key using this code:

    Sub AutoOpen()
        CustomizationContext = ActiveDocument
        KeyBindings.Add wdKeyCategoryMacro, "ProcessTable", BuildKeyCode(wdKeyCommand, wdKeyR)
    End Sub

    Everything was working fine for a few weeks, but this morning she emailed to say that she couldn't type a captial R in the table unless she turned on caps lock. She sent me her copy of the template, and I can confirm the problem. But, on my system with the original version of the template everything still works fine.

    So of course I sent her another copy of the original template, but when she opened it on her system she had the same "R invoking CmdR" behavior. She saved the document, sent it back to me, and I can duplicate the problem in her version of my version. (Does that make sense?)

    It's obviously something that's happening when she opens the template on her machine, but it doesn't happen to her with any other document or template, so it isn't (or doesn't appear to be) anything global that she's set up by accident in Normal.dot or something. But it doesn't seem to be an installation-specific issue, either, because I can duplicate the problem when she sends me her version.

    I don't even know where to begin to figure out where or what the problem is. Shortcut keys are assigned in so many ways and so many places in Word these days, and I can't find a "tell me what you think R means" dialog anywhere. And I can't seem to do a document compare that includes the macros.

    Oh, and to make things really interesting, she's running Office:Mac and I'm running Office 2010 on Windows. But it was working just fine for a couple of weeks, and we all know that code doesn't spontaneously combust, so....um....help? Please?


    Rebecca M. Riordan

    Wednesday, May 30, 2012 6:25 PM

Answers

  • <<

    It still ran the original macro THAT WASN'T EVEN PRESENT IN THE DOCUMENT.

    >>

    It's possible that there is another ProcessTable macro in Normal.dotm or the attached template. (Off the top of my head, I thought the key was bound to a particular macro in a particular object, but I'd have to revisit that area to check what Word looks for and in what sequence). If it isn't that, that would indeed be scary!

    You can check the key bindings for a particular macro on Word 2011 by looking at Tools->Customize Keyboard..., selecting category Macros (in the left hand column), selecting each place the customization might have been made in in the Save Changes In dropdown, then selecting macros such as "ProcessTable", "ThisDocument.ProcessTable" in the list on the right. Each macro should display any bound keys, including "plain" keys such as "R". Once displayed, you should be able to remove that binding by selecting "R" and clicking the Delete button.

    You can probably do much the same thing in Word 2011 VBA using the following macro:

    Sub clearCustomizationR()
    Dim c As Long
    Application.CustomizationContext = NormalTemplate
    'Debug.Print "Normal template: " & KeyBindings.Count
    For c = 1 To KeyBindings.Count
      If KeyBindings(c).KeyString = "R" Then
        KeyBindings(c).Clear
        Exit For
      End If
    Next
    If ActiveDocument.AttachedTemplate <> NormalTemplate.Name Then
      'Application.CustomizationContext = ActiveDocument.AttachedTemplate
      Debug.Print "Attached template: " & KeyBindings.Count
      For c = 1 To KeyBindings.Count
        If KeyBindings(c).KeyString = "R" Then
          KeyBindings(c).Clear
          Exit For
        End If
      Next
    End If

    Application.CustomizationContext = ActiveDocument
    'Debug.Print "Active document: " & KeyBindings.Count
    For c = 1 To KeyBindings.Count
      If KeyBindings(c).KeyString = "R" Then
        Debug.Print KeyBindings(c).Command
        KeyBindings(c).Clear
        Exit For
      End If
    Next
    End Sub

    It may be worth checking that you have not also got multiple AutoOpen subroutines (in Normal, in a template, in the document).

    (NB, you can set a plain key binding in VBA even though you cannot do it in the U.I., e.g.

    KeyBindings.Add wdKeyCategoryMacro, "ProcessTable", wdKeyR

    )

    You can also specify a particular location for the macro you want to bind to, e.g.

    KeyBindings.Add wdKeyCategoryMacro, "Normal.ThisDocument.ProcessTable", wdKeyR

    and so on.



    Peter Jamieson

    Tuesday, June 5, 2012 2:07 PM

All replies

  • Hi Rebecca,

    Thanks for posting in the MSDN Forum.

    I will involve some experts into this thread to see whether they can help you out. There might be some time delay, appreciate for your patience.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 31, 2012 4:51 AM
    Moderator
  • Thanks, Tom. Glad to know it isn't something obvious (or you would have caught it). I'll await further results...

    Rebecca M. Riordan

    Thursday, May 31, 2012 2:04 PM
  • Are you providing a "proper" template (.dotm) or a document (.docm) ? (I ask because I'd expect to use AutoNew rather than AutoOpen if it was a .dotm)

    Either way, I would suggest that you create your own .docm from your template, i.e. so that the AutoOpen runs. Then compare the XML of that file with the one that your friend has sent back:

     a. rename to .zip

     b. navigate inside the .zip

     c. open the word folder inside the .zip. You should see a number of files including customizations.xml.

    Compare the contents of customizations.xml for the two files.

    There will probably be at least one difference anyway - here, running your VBA on Word 2010 the resulting file contains

    <wne:keymaps><wne:keymap wne:kcmPrimary="0252">

    In the Word 2011 file, I see

     <wne:keymaps><wne:keymap wne:kcmPrimary="0152">

    which I assume probably reflects the Mac use of cmd rather than ctrl, but I do not know for sure.

    That may not provide an explanation for what has gone wrong but it may at least show a significant change between your original and the non-working version.




    Peter Jamieson

    Thursday, May 31, 2012 2:22 PM
  • Hi Rebecca

    I'm a bit confused on one point: Usually, you wouldn't hand a template (*.dotm) to someone and have them open it. The idea with a template is that it's placed in the central templates location and the user goes over File/New to access it. This generates a copy of the file as a "plain document". The document will keep a link to the template and be able to access macros and keyboard shortcuts contained in it. In this case, the macros won't travel with the document.

    So it's not clear to me exactly what your scenario is. However...

    If you manually create a keyboard shortcut in a document (or template), saving the key assignment in that document (rather than Normal.dotm, for instance), that shortcut should stay active. There wouldn't be any reason for you to need to continually recreate the shortcut.

    Note: IN the Keyboardshortcut dialog box you can also see what is assigned to a keyboard combination. If you select a command, click in the box for the shortcut to assign to it and press the shortcut information will be displayed that tells you to what it's currently assigned (or it will tell you it's unassigned).


    Cindy Meister, VSTO/Word MVP

    Thursday, May 31, 2012 4:50 PM
    Moderator
  • Actually, I now realise that if you open your document in both Windows and Word versions of Mac, you will get both those <keymap> entries, and each seems to treat the other's as the entry for "R", which would explain the problem. The solution is probably along the lines CIndy has suggested, or anything that would prevent the customisation from spreading.

    This makes sense on the WIndows side because the documentation at

    http://msdn.microsoft.com/en-us/library/ff534689(v=office.12)

    says that the "01" in "0152" should be for shift-R, and the "02" in "0252" should be for ctrl-R.

    The question really is why Mac Word is setting the "01" when we specify cmd-R, and why it is interpreting "02" as shift-R. It seems like an error on the Mac side to me. The bit of documentation that says

    "(where the operating system distinguishes between Command and Control)"

    seems, at best, to be ambiguous.



    Peter Jamieson

    Friday, June 1, 2012 10:30 PM
  • Cindy, thanks for your response. I do understand the difference between templates and macro-enabled documents, and we're getting the same results with both.

    I did try the keyboard shortcut dialog, and it's telling not responding to Shift+R at all. (Which of course is sensible, since Shift+R isn't a valid shortcut key).


    Rebecca M. Riordan

    Saturday, June 2, 2012 11:55 AM
  • Peter,

    Thanks for your help. I had no idea about changing the extension to see the file as a, well, I'll call it a "project" because that's my background. That's very useful information, and I thank you for it.

    You're right in your suspicions. Her version shows 152 and 252, while mine shows 252 and 258. I can't do binary-to-decimal conversions in my head, but that's obviously the problem. Or rather the symptom of the problem. The question now is why is getting changed with this template (or doc, as I said to Cindy) and only with this one, and why has it happened now after working for several weeks...harumph.

    Unless someone has a better idea, the only thing I can think to try now is to start with a new, clean template and see if we can get it to break again.

    BTW, the AutoOpen code is a snippet I copied from MSDN somewhere. (This is soooo not my area of expertise!) I'll try AutoNew and see if that makes any difference. (Keep your fingers crossed!)


    Rebecca M. Riordan

    Saturday, June 2, 2012 12:03 PM
  • <<

    The question now is why is getting changed with this template (or doc, as I said to Cindy) and only with this one, and why has it happened now after working for several weeks...harumph.

    >>

    Yes, that wasn't obvious to me either. I made an interim assumption that at some point your friend must have opened the doc on a Windows system too, or sent it to someone who did that and sent it back for further work.

    Another thing that might be relevant is that the autoopen and other auto/event macros do not always fire on Mac word. Actually, it is not obvious how cmd-R would work if your autoopen did not run, but for further info.  see Rich Michaels' post in the following thread:

    http://social.technet.microsoft.com/Forums/en-US/word/thread/07cadeb4-de60-4f51-b2af-1f4b5e4fcbef/

    > BTW, the AutoOpen code is a snippet I copied from MSDN somewhere. 

    FWIW, the vast majority of the code samples on the web are about the Windows version, and most probably have not been tested on the Mac version, and even when they have been, may not take into account the type of portability issue you have encountered here. 


    Peter Jamieson

    Tuesday, June 5, 2012 10:53 AM
  • <<Another thing that might be relevant is that the autoopen and other auto/event macros do not always fire on Mac word. Actually, it is not obvious how cmd-R would work if your autoopen did not run, but for further info.  see Rich Michaels' post in the following thread:>>

    Again, Peter, I thank you for useful information.

    But it's gotten odder than that. In attempt to track down the problem, I created a new template that just inserted our surnames in the table when she pressed Cmd+r. It still ran the original macro THAT WASN'T EVEN PRESENT IN THE DOCUMENT. And she still isn't having the problem in other documents. It's like Word has decided to mis-behave if the document was created by me. That's just scary.

    I thought it might be a caching problem, so I had her re-boot. Then nothing ran in the new tracing template. That might have been the autonew not firing.

    I thought maybe it was something odd about the "r". So I had her re-boot and sent her a version that used "x", instead. It worked fine. So, I changed my original version (working, neve opened on her machine) to use "x" instead of "r" and sent it to her. She still couldn't type a capital r (which isn't referenced in the template) or a capital x.

    I've asked her to re-install Word, and re-sent her the uncorrupted version of the template.

    If that doesn't work, the only thing I can think is to create a completely new version of the document, without any shortcut keys. If she gets her "r" back, then I'll have her run the macro from the Developer tab instead of a shortcut key. It's not pretty, but we're both working on book deadlines at the moment, so...


    Rebecca M. Riordan

    Tuesday, June 5, 2012 11:16 AM
  • <<

    It still ran the original macro THAT WASN'T EVEN PRESENT IN THE DOCUMENT.

    >>

    It's possible that there is another ProcessTable macro in Normal.dotm or the attached template. (Off the top of my head, I thought the key was bound to a particular macro in a particular object, but I'd have to revisit that area to check what Word looks for and in what sequence). If it isn't that, that would indeed be scary!

    You can check the key bindings for a particular macro on Word 2011 by looking at Tools->Customize Keyboard..., selecting category Macros (in the left hand column), selecting each place the customization might have been made in in the Save Changes In dropdown, then selecting macros such as "ProcessTable", "ThisDocument.ProcessTable" in the list on the right. Each macro should display any bound keys, including "plain" keys such as "R". Once displayed, you should be able to remove that binding by selecting "R" and clicking the Delete button.

    You can probably do much the same thing in Word 2011 VBA using the following macro:

    Sub clearCustomizationR()
    Dim c As Long
    Application.CustomizationContext = NormalTemplate
    'Debug.Print "Normal template: " & KeyBindings.Count
    For c = 1 To KeyBindings.Count
      If KeyBindings(c).KeyString = "R" Then
        KeyBindings(c).Clear
        Exit For
      End If
    Next
    If ActiveDocument.AttachedTemplate <> NormalTemplate.Name Then
      'Application.CustomizationContext = ActiveDocument.AttachedTemplate
      Debug.Print "Attached template: " & KeyBindings.Count
      For c = 1 To KeyBindings.Count
        If KeyBindings(c).KeyString = "R" Then
          KeyBindings(c).Clear
          Exit For
        End If
      Next
    End If

    Application.CustomizationContext = ActiveDocument
    'Debug.Print "Active document: " & KeyBindings.Count
    For c = 1 To KeyBindings.Count
      If KeyBindings(c).KeyString = "R" Then
        Debug.Print KeyBindings(c).Command
        KeyBindings(c).Clear
        Exit For
      End If
    Next
    End Sub

    It may be worth checking that you have not also got multiple AutoOpen subroutines (in Normal, in a template, in the document).

    (NB, you can set a plain key binding in VBA even though you cannot do it in the U.I., e.g.

    KeyBindings.Add wdKeyCategoryMacro, "ProcessTable", wdKeyR

    )

    You can also specify a particular location for the macro you want to bind to, e.g.

    KeyBindings.Add wdKeyCategoryMacro, "Normal.ThisDocument.ProcessTable", wdKeyR

    and so on.



    Peter Jamieson

    Tuesday, June 5, 2012 2:07 PM
  • Peter, you are my hero. I owe you lunch. (Even better, my friend owes you lunch, and she's an award-winning cookbook author)

    I added your clear loop to the AutoNew(), changed "R" to "Shift+R" (easily found using a watch), and bingo, it's working. Not elegant, but it works, and at that point that's the important thing. (My father used to call this sort of thing "brute finesse".)

    I still don't know how or why it happened (and of course I'd like to, just out of curiosity), but the important thing is that it works, and if it happens again, I know how to tackle it.

    Thank you SOOO MUCH.

    PS

    I'm currently suspecting something in her Normal (which it turns out she kept when she re-installed), but she can't find it, and I don't know how to tell her where to look on a Mac, so I'm going to cross my fingers and let it go...


    Rebecca M. Riordan

    Tuesday, June 5, 2012 3:01 PM
  • so I'm going to cross my fingers and let it go...

    Probably the best thing at this point. But

     a. on a typical system she will find her template under her "Library" folder, here:

    Macintosh HD:Users:herusername:Library:Application Support:Microsoft:Office:User Templates:Normal.dotm

    (Mac people might also show this as follows:

    ~/Library/Application Support/Microsoft/Office/User Templates/Normal.dotm

    )

    If she is using Mac OS X Lion, to open her Library folder in the Finder she can hold down the "Option" (Alt) key and click the FInder's "Go" menu.

     b. If she can use the VBA Editor, typing the following in the Immediate window should give the correct location/name

    ?normaltemplate.FullName

     c. Alternatively, she could insert a { TEMPLATE \p } field in a new blank document and show the result. It's a bit harder to give reliable instructions for that on Mac, but let me know if it gets that far.


    Peter Jamieson

    Tuesday, June 5, 2012 4:32 PM
  • Peter,

    Again, thank you. I'll pass this on to Rose. And she's asked me to pass this on to you:

    Rebecca and Peter, you are both my heroes! I had all but given up hope and decided that the best solution was to retrain myself not to use the shift key for text as word gives me the option to have the first letter of every sentence capitalized.

     Rejoicing in how many keystrokes this would save over the period of a life-time along came the new template with the solution. I still think I will avoid using the shift key, except when in the middle of a sentence, but am so happy that I don’t have to!

     Thank you so much for this beautiful solution. And yes Peter—I definitely owe you a dinner. Are you in NY?

    If you want to take her up on her invitation, you can email me at rebeccamarye at msn dot com and I'll put the two of you in touch. (I, unfortunately, am NOT in NY!)


    Rebecca M. Riordan

    Tuesday, June 5, 2012 8:14 PM