Ask Your Question

How can I write a macro to assign a shortcut to another macro?

asked 2016-09-10 11:55:35 +0200

Ulli_Wue gravatar image

updated 2016-09-10 12:26:43 +0200

I'm wondering if it is possible to create a macro which itself assigns a list of existing macros to another existing lists of shortscuts? Macro recorder was no help. I know how to bind a macro to a shortcut over the menue: Tools --> Customize --> Keyboard .... But writing a macro for this task, I even don't know where to start .... Any ideas?

edit retag flag offensive close merge delete


Seems to me that you should start by reading this page

JohnSUN gravatar imageJohnSUN ( 2016-09-10 13:12:25 +0200 )edit

JohnSun, thank you, I read it, hmmmm, still no improvement ...

Some years ago I wrote in VBA this to assign my macros to a shortcut list

Sub AssignShortCuts()
m = Array("macro1", "macro2", "macro3")
s = Array("a", "b", "c")
With Application
For i = 0 To UBound(m)
.MacroOptions Macro:=m(i), HasShortcutKey:=True, ShortcutKey:=si)
End With
End Sub

Ulli_Wue gravatar imageUlli_Wue ( 2016-09-10 13:20:25 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-06-23 01:59:07 +0200

librebel gravatar image

Hello @Ulli_Wue, you could use the following code to assign a keyboard shortcut to a macro:

Sub Main()
    REM Set your Macro URL and your Keyboard Shortcut here:
    Dim strCommandURL$, oKeyEvent
    strCommandURL = ""
    oKeyEvent = CreateKeyEvent( 2, )     REM Ctrl-J

    SetCommandShortcut( oKeyEvent, strCommandURL )
    'RemoveCommandShortcut( strCommandURL )
End Sub

Function getShortCutManager()
REM Return the ShortCutManager for the current Office Module.
    Dim oModuleManager As Object, oModuleIdent
    Dim oModuleConfigManager As Object, oModuleConfigManagerSupplier As Object
    oModuleManager = createUnoService( "" )
    oModuleIdent = oModuleManager.identify( ThisComponent )
    oModuleConfigManagerSupplier = createUnoService( "" )
    oModuleConfigManager = oModuleConfigManagerSupplier.getUIConfigurationManager( oModuleIdent )
    getShortCutManager = oModuleConfigManager.getShortCutManager()
End Function

Sub SetCommandShortcut( oKeyEvent, strCommandURL as String )
REM Adapted from code by Paolo Mantovani.
REM Connects a Keyboard Shortcut to a certain Command, such as a macro or UNO dispatch.
REM   <oKeyEvent>: representing the Keyboard Shortcut for this Command.
REM   <strCommandURL>:  the Command to which the Keyboard Shortcut will be attached.
REM Example call:
REM strCommandURL = ""
REM oKeyEvent = CreateKeyEvent( 2, )     REM Ctrl-J
REM SetCommandShortcut( oKeyEvent, strCommandURL )
    Dim oShortCutManager As Object
    oShortCutManager = getShortCutManager()
    oShortCutManager.setKeyEvent( oKeyEvent, strCommandURL )
End Sub

Sub RemoveCommandShortcut( strCommandURL as String )
REM Removes all the Keyboard Shortcut(s) associated with the specified Command.
REM <strCommandURL>: a Command that has one or more Keyboard Shortcuts to be removed from it.
    Dim oShortCutManager As Object
    oShortCutManager = getShortCutManager()
    oShortCutManager.removeCommandFromAllKeyEvents( strCommandURL )
End Sub

Function CreateKeyEvent( iModifiers as Integer, iKeyCode as Integer ) As
REM Construct and return a KeyEvent structure.
    Dim aKeyEvent As New
    aKeyEvent.Modifiers = iModifiers
    aKeyEvent.KeyCode = iKeyCode
    CreateKeyEvent = aKeyEvent
End Function
edit flag offensive delete link more
Login/Signup to Answer

Question Tools



Asked: 2016-09-10 11:55:35 +0200

Seen: 545 times

Last updated: Jun 23 '17