Ask Your Question
0

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

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

Ulli_Wue gravatar image

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

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

Comments

Seems to me that you should start by reading this page

JohnSUN gravatar imageJohnSUN ( 2016-09-10 13:12:25 +0100 )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)
Next
End With
End Sub

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

1 Answer

Sort by » oldest newest most voted
0

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

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 = "vnd.sun.star.script:Standard.Module1.yourMacroName?language=Basic&location=document"
    oKeyEvent = CreateKeyEvent( 2, com.sun.star.awt.Key.J )     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( "com.sun.star.frame.ModuleManager" )
    oModuleIdent = oModuleManager.identify( ThisComponent )
    oModuleConfigManagerSupplier = createUnoService( "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" )
    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>:      com.sun.star.awt.KeyEvent representing the Keyboard Shortcut for this Command.
REM   <strCommandURL>:  the Command to which the Keyboard Shortcut will be attached.
REM Example call:
REM strCommandURL = "vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=document"
REM oKeyEvent = CreateKeyEvent( 2, com.sun.star.awt.Key.J )     REM Ctrl-J
REM SetCommandShortcut( oKeyEvent, strCommandURL )
    Dim oShortCutManager As Object
    oShortCutManager = getShortCutManager()
    oShortCutManager.setKeyEvent( oKeyEvent, strCommandURL )
    oShortCutManager.store()
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 ) 
    oShortCutManager.store()
End Sub

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

Question Tools

2 followers

Stats

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

Seen: 293 times

Last updated: Jun 23 '17