Here is the code I modified from Apache OpenOffice Community Forum - Custom Cell Background - (View topic) to use in my library. The difference in adding the items using this macro versus Tools > Customize
is that,
- If a macro is mapped to keyboard shortcut, the shortcut don’t show on the popup menu.
- The change is for active document only.
- The change is not permanent. Needs to be reloaded when you next open the document again. This can be automated using Document Open event in
Tools > Customize
- According to the original author, you can’t add a real separator using his code Best case is a blank item.
- This macro adds items to the context menu of both text selection and spelling errors. Where as @sokol92’s solution and manually doing via Tools menu only adds it for text selection.
Here is the code,
' Context Menu globals...
Global oDocView As Object
Global oContextMenuInterceptor As Object
Global oStore As Object
Global oPropSetRegistry As Object
Const MNU_PREFIX = "MyContextMenu_"
'_______________________________________________________________________________
'
Sub registerContextMenuInterceptor
InitMenuFactory
oDocView = ThisComponent.CurrentController
oContextMenuInterceptor = CreateUnoListener("oTextDoc_", "com.sun.star.ui.XContextMenuInterceptor")
oDocView.registerContextMenuInterceptor(oContextMenuInterceptor)
End Sub '/registerContextMenuInterceptor
'_______________________________________________________________________________
'
Sub releaseContextMenuInterceptor
On Error Resume Next
oDocView.releaseContextMenuInterceptor(oContextMenuInterceptor)
TerminateMenuFactory
End Sub '/releaseContextMenuInterceptor
'_______________________________________________________________________________
'
Function oTextDoc_notifyContextMenuExecute(ContextMenuExecuteEvent As Object) As Variant
Dim oSrcWin As Object
Dim oExePoint As Object
Dim oATContainer As Object
Dim oSelection As Object
Dim oMenuItem As Object
Dim I As Integer
Dim NextEntry As Integer
Dim t As String
With ContextMenuExecuteEvent
'contains the window where the context
'menu has been requested
oSrcWin = .SourceWindow
'contains the position the context menu
'will be executed at (css.awt.Point)
oExePoint = .ExecutePosition
'enables the access to the menu content.
'The implementing object has to support the
'service ActionTriggerContainer
oATContainer = .ActionTriggerContainer
'provides the current selection
'inside the source window
oSelection = .Selection
End With
NextEntry = oATContainer.Count
Const macroPrefix = "macro:///MyLibrary.Module1"
oMenuItem = GetSimpleMenuItem("NewEntry1", "Google Text", _
macroPrefix & ".googleSelection")
oATContainer.insertByIndex(NextEntry, oMenuItem)
oMenuItem = GetSimpleMenuItem("NewEntry2", "Google As Quoted Text", _
macroPrefix & ".googleSelectionQuoted")
oATContainer.insertByIndex(NextEntry + 1, oMenuItem)
oMenuItem = GetSimpleMenuItem("NewEntry3", "Google Translate", _
macroPrefix & ".googleTranslate")
oATContainer.insertByIndex(NextEntry + 2, oMenuItem)
' POSSIBLE RESULTS FOR THIS FUNCTION
' This function must result one of the following values:
' com.sun.star.ui.ContextMenuInterceptorAction.IGNORED
' the XContextMenuInterceptor has ignored the call.
' The next registered XContextMenuInterceptor should be notified.
' com.sun.star.ui.ContextMenuInterceptorAction.CANCELLED
' the context menu must not be executed.
' The next registered XContextMenuInterceptor should not be notified.
' com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED
' the menu has been modified and should be executed
' without notifying the next registered XContextMenuInterceptor.
' com.sun.star.ui.ContextMenuInterceptorAction.CONTINUE_MODIFIED
' the menu has been modified and the next registered
' XContextMenuInterceptor should be notified.
oTextDoc_notifyContextMenuExecute = com.sun.star.ui.ContextMenuInterceptorAction.EXECUTE_MODIFIED
End Function '/oTextDoc_notifyContextMenuExecute
'_______________________________________________________________________________
'
' MENU FACTORY ROUTINES
'_______________________________________________________________________________
'
Sub InitMenuFactory()
oStore = CreateUnoService("com.sun.star.ucb.Store")
oPropSetRegistry = oStore.createPropertySetRegistry("")
End Sub '/InitMenuFactory
'_______________________________________________________________________________
'
Sub TerminateMenuFactory()
Dim mNames()
Dim sName As String
Dim i As Integer
mNames() = oPropSetRegistry.getElementNames
For i = LBound(mNames()) To UBound(mNames())
sName = mNames(i)
If Left(sName, Len(MNU_PREFIX)) = MNU_PREFIX Then
oPropSetRegistry.removePropertySet(sName)
End If
Next i
oPropSetRegistry.dispose
oStore.dispose
End Sub '/TerminateMenuFactory
'
' Sorry: menu icon and sub-menues not supported (maybe later)
'_______________________________________________________________________________
'
Function GetSimpleMenuItem( sName As String, sText As String, sCommandUrl As String, Optional sHelpUrl As String ) As Object
Dim oPropSet As Object
Dim sInternalName As String
sInternalName = MNU_PREFIX & sName
If oPropSetRegistry.hasByName(sInternalName) Then
oPropSetRegistry.removePropertySet(sInternalName)
End If
oPropSet = oPropSetRegistry.openPropertySet(sInternalName, True)
oPropSet.addProperty("Text", 0, sText)
oPropSet.addProperty("CommandURL", 0, sCommandUrl)
If Not IsMissing(sHelpUrl) Then
oPropSet.addProperty("HelpURL", 0, sHelpUrl)
End If
GetSimpleMenuItem = oPropSet
End Function '/GetSimpleMenuItem
'_______________________________________________________________________________
'
Function GetMenuSeparator( sName As String ) As Object
Dim oPropSet As Object
Dim sInternalName As String
Dim iSeparatorType As Integer
sInternalName = MNU_PREFIX & sName
If oPropSetRegistry.hasByName(sInternalName) Then
oPropSetRegistry.removePropertySet(sInternalName)
End If
oPropSet = oPropSetRegistry.openPropertySet(sInternalName, True)
'constant group com.sun.star.ui.ActionTriggerSeparatorType not supported?
'unfortunately, the only separator-type working is the "SPACE"
'regardless for the iSeparatorType passed...
iSeparatorType = 1
oPropSet.addProperty("SeparatorType", 0, iSeparatorType)
GetMenuSeparator = oPropSet
End Function '/GetMenuSeparator