none
Redirect "Ctrl + P" to Custom Tab In Backstage View RRS feed

  • Question

  • I have created my own custom "Quick Print" Tab in the backstage view that has all the required settings the users need in order to quickly print documents on different paper types.

    I have not removed the original "Print" tab in the backstage view so when I use the shortcut keys "Ctrl + P" it refers me to the built-in Print tab.

    My query is - how can I change the shortcut keys "Ctrl + P" to refer to my custom "Quick Print" tab?

    Any help would be much appreciated!

    Louis

    Tuesday, May 22, 2012 2:35 PM

Answers

  • Hi Louis

    I don't think there is any reliable way to link a keyboard shortcut with a Backstage tab, because there's no way to programmatically "call" a backstage tab.

    Closest you could get would be a macro that uses SendKeys to execute the assigned keytip, then link Ctrl+P to that macro. The problem is that you can't absolutely guarantee that the keytip you assign will actually be used by Word...


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by LCJ187 Friday, May 25, 2012 4:00 PM
    Wednesday, May 23, 2012 2:22 PM
    Moderator
  • Thanks Cindy,

    Your method was a bit tricky but I did manage to get it working in the end. Thank you very much! The tricky part was getting the macro to wait until the user had let go of the Ctrl key, to then run the code for the sendkeys (using sleep did not work).

    I have included what I did and my code below just to help anyone who may have a similar problem -

    In Word -

    Change the keyboard shortcut for a macro
    1. Click the Microsoft Office Button , and then click Word Options.
    2. Click Customize.
    3. Next to Keyboard shortcuts, click Customize.
    4. In the Categories list, click Macros.
    5. In the Macros list, click the macro that you want to change.
    6. In the Press new shortcut key box, type the key combination that you want to choose.
    7. Check the Current keys box to make sure that you aren't assigning a key combination that you already use to perform a different task.
    8. In the Save changes in list, click the option that matches where you want to run your macro.

      Important   To make your macro available in all documents, be sure to clickNormal.dotm.

    9. Click Close.

     

    Public Enum tKeyCodes
       KeyCodesShift = &H10
       KeyCodesCtrl = &H11
       KeyCodesAlt = &H12
       KeyCodesCapsLock = &H14
       KeyCodesNumLock = &H90
       KeyCodesScrollLock = &H91
    End Enum

    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
    Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)

    Sub docPrintShortcutKey()

    Call SendIt
    Call returnToBRPrintQAT

    End Sub

    Sub returnToBRPrintQAT()

    'Returns to the Quick Print tab
    Application.PrintPreview = False
    SendKeys ("%f"), True
    SendKeys ("%BP6"), True
    SendKeys ("%"), True

    End Sub

    Public Sub Pause(ByVal Seconds As Single, Optional ByVal PreventVBEvents As Boolean)

       Const MaxSystemSleepInterval = 25 'milliseconds
       Const MinSystemSleepInterval = 1  'milliseconds
      
       Dim ResumeTime As Double
       Dim Factor As Long
       Dim SleepDuration As Double
      
       Factor = CLng(24) * 60 * 60
      
       ResumeTime = Int(Now) + (Timer + Seconds) / Factor
      
       Do
          SleepDuration = (ResumeTime - (Int(Now) + Timer / Factor)) * Factor * 1000
          If SleepDuration > MaxSystemSleepInterval Then SleepDuration = MaxSystemSleepInterval
          If SleepDuration < MinSystemSleepInterval Then SleepDuration = MinSystemSleepInterval
          Sleep SleepDuration
          If Not PreventVBEvents Then DoEvents
       Loop Until Int(Now) + Timer / Factor >= ResumeTime
      
    End Sub

     

    Public Function IsKeyPressed(ByVal Key As tKeyCodes) As Boolean
      
    ' Return True if the requested key is pressed or active, False otherwise.

       Dim Result As Long

       Result = GetKeyState(Key)
      
       ' Toggle keys use the first bit while the others use the 16th bit
       Select Case Key
          Case KeyCodesCapsLock, KeyCodesNumLock, KeyCodesScrollLock
             IsKeyPressed = Result And &H1
          Case Else
             IsKeyPressed = Result And &H8000
       End Select

    End Function


    Public Sub SendIt()
        DoEvents
        While IsKeyPressed(KeyCodesShift) Or IsKeyPressed(KeyCodesCtrl)
            Pause 0.25
        Wend
    End Sub

    Friday, May 25, 2012 3:57 PM

All replies

  • Hi Louis

    I don't think there is any reliable way to link a keyboard shortcut with a Backstage tab, because there's no way to programmatically "call" a backstage tab.

    Closest you could get would be a macro that uses SendKeys to execute the assigned keytip, then link Ctrl+P to that macro. The problem is that you can't absolutely guarantee that the keytip you assign will actually be used by Word...


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by LCJ187 Friday, May 25, 2012 4:00 PM
    Wednesday, May 23, 2012 2:22 PM
    Moderator
  • Thanks Cindy,

    Your method was a bit tricky but I did manage to get it working in the end. Thank you very much! The tricky part was getting the macro to wait until the user had let go of the Ctrl key, to then run the code for the sendkeys (using sleep did not work).

    I have included what I did and my code below just to help anyone who may have a similar problem -

    In Word -

    Change the keyboard shortcut for a macro
    1. Click the Microsoft Office Button , and then click Word Options.
    2. Click Customize.
    3. Next to Keyboard shortcuts, click Customize.
    4. In the Categories list, click Macros.
    5. In the Macros list, click the macro that you want to change.
    6. In the Press new shortcut key box, type the key combination that you want to choose.
    7. Check the Current keys box to make sure that you aren't assigning a key combination that you already use to perform a different task.
    8. In the Save changes in list, click the option that matches where you want to run your macro.

      Important   To make your macro available in all documents, be sure to clickNormal.dotm.

    9. Click Close.

     

    Public Enum tKeyCodes
       KeyCodesShift = &H10
       KeyCodesCtrl = &H11
       KeyCodesAlt = &H12
       KeyCodesCapsLock = &H14
       KeyCodesNumLock = &H90
       KeyCodesScrollLock = &H91
    End Enum

    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
    Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)

    Sub docPrintShortcutKey()

    Call SendIt
    Call returnToBRPrintQAT

    End Sub

    Sub returnToBRPrintQAT()

    'Returns to the Quick Print tab
    Application.PrintPreview = False
    SendKeys ("%f"), True
    SendKeys ("%BP6"), True
    SendKeys ("%"), True

    End Sub

    Public Sub Pause(ByVal Seconds As Single, Optional ByVal PreventVBEvents As Boolean)

       Const MaxSystemSleepInterval = 25 'milliseconds
       Const MinSystemSleepInterval = 1  'milliseconds
      
       Dim ResumeTime As Double
       Dim Factor As Long
       Dim SleepDuration As Double
      
       Factor = CLng(24) * 60 * 60
      
       ResumeTime = Int(Now) + (Timer + Seconds) / Factor
      
       Do
          SleepDuration = (ResumeTime - (Int(Now) + Timer / Factor)) * Factor * 1000
          If SleepDuration > MaxSystemSleepInterval Then SleepDuration = MaxSystemSleepInterval
          If SleepDuration < MinSystemSleepInterval Then SleepDuration = MinSystemSleepInterval
          Sleep SleepDuration
          If Not PreventVBEvents Then DoEvents
       Loop Until Int(Now) + Timer / Factor >= ResumeTime
      
    End Sub

     

    Public Function IsKeyPressed(ByVal Key As tKeyCodes) As Boolean
      
    ' Return True if the requested key is pressed or active, False otherwise.

       Dim Result As Long

       Result = GetKeyState(Key)
      
       ' Toggle keys use the first bit while the others use the 16th bit
       Select Case Key
          Case KeyCodesCapsLock, KeyCodesNumLock, KeyCodesScrollLock
             IsKeyPressed = Result And &H1
          Case Else
             IsKeyPressed = Result And &H8000
       End Select

    End Function


    Public Sub SendIt()
        DoEvents
        While IsKeyPressed(KeyCodesShift) Or IsKeyPressed(KeyCodesCtrl)
            Pause 0.25
        Wend
    End Sub

    Friday, May 25, 2012 3:57 PM