Hello!
I want to add a menu in OO Writer, when my Application is running. I think i can do this with a macro. But which objects do i need? I found an old example in the oooforum but it shows the menus only after a restart of oowriter. And i dont want to make a restart of oowriter by my application.
I have recently answered this question. Please look at the following basic macro which adds a new command to the "File" popup menu. The changed menu bar is accessible without a restart. Please keep in mind that the macro only changes the menu bar of the current frame non-persistently.
Regards, Carsten
-------- REM ***** BASIC *****
Sub Main REM *** Adds a item to the File Menu REM *** Initialize strings sMenuBar = "private:resource/menubar/menubar" sMyPopupMenuCmdId = ".uno:PickList" sMyCommand = "macro:///Standard.Module1.Test()"
REM *** Retrieve the current frame of my model oModel = ThisComponent
if not isNull( oModel ) then
REM *** Retrieve frame from current controller
oFrame = oModel.getCurrentController().getFrame() REM *** Retrieve the layout manager of my current frame
oLayoutManager = oFrame.LayoutManager() REM *** Retrieve the menu bar from the layout manager
oMenuBar = oLayoutManager.getElement( sMenuBar ) REM *** Retrieve writable configuration settings from menu bar
oMenuBarSettings = oMenuBar.getSettings( true ) REM *** Make our changes only transient. An Add-on should
REM *** never change configuration persistently as it can
REM *** be deinstalled by a user without any chance to
REM *** undo its configuration changes!
REM *** Please look for bug #i46194 which prevents using
REM *** oMenuBar.Persistent = false!! Is fixed on build
REM *** SRC680m91 or newer.
oMenuBar.Persistent = falseREM *** Look for the "File" popup menu and add our command
REM *** We must look if we haven't added our command already!
fileMenuIndex = FindPopupMenu( sMyPopupMenuCmdId, oMenuBarSettings )
if fileMenuIndex >= 0 then
oPopupMenuItem() = oMenuBarSettings.getByIndex(fileMenuIndex)
oPopupMenu = GetProperty( "ItemDescriptorContainer", oPopupMenuItem() )
if not isNull( oPopupMenu ) then
if FindCommand( sMyCommand, oPopupMenu ) = -1 then
oMenuItem = CreateMenuItem( sMyCommand, "Standard.Module1.Test" )
nCount = oPopupMenu.getCount()
oPopupMenu.insertByIndex( nCount, oMenuItem )
endif
endif
else
msgbox "No file menu found!"
endif
oMenuBar.setSettings( oMenuBarSettings ) endif End Sub
Function FindCommand( Command as String, oPopupMenu as Object ) as Integer
nCount = oPopupMenu.getCount()-1
for i = 0 to nCount
oMenuItem() = oPopupMenu.getByIndex(i)
nPropertyCount = ubound(oMenuItem())
for j = 0 to nPropertyCount
if oMenuItem(j).Name = "CommandURL" then
if oMenuItem(j).Value = Command then
FindCommand = j
exit function
endif
endif
next j
next i
FindCommand = -1
End FunctionFunction FindPopupMenu( Command as String, oMenuBarSettings as Object ) as Integer
for i = 0 to oMenuBarSettings.getCount()-1
oPopupMenu() = oMenuBarSettings.getByIndex(i)
nPopupMenuCount = ubound(oPopupMenu())
for j = 0 to nPopupMenuCount
if oPopupMenu(j).Name = "CommandURL" then
if oPopupMenu(j).Value = Command then
FindPopupMenu = j
exit function
endif
endif
next j
next i
FindPopupMenu = -1
End Function
Function GetProperty( PropertyName as String, properties() as Variant ) as Variant
for j = lbound( properties() ) to ubound( properties() )
oPropertyValue = properties(j)
if oPropertyValue.Name = PropertyName then
GetProperty = oPropertyValue.Value
exit function
endif
next j
GetProperty = null
end function
Function CreateMenuItem( Command as String, Label as String ) as Variant
Dim aMenuItem(2) as new com.sun.star.beans.PropertyValueaMenuItem(0).Name = "CommandURL" aMenuItem(0).Value = Command aMenuItem(1).Name = "Label" aMenuItem(1).Value = Label aMenuItem(2).Name = "Type" aMenuItem(2).Value = 0
CreateMenuItem = aMenuItem() End Function
Function CreatePopupMenu( CommandId, Label, Factory ) as Variant
Dim aPopupMenu(3) as new com.sun.star.beans.PropertyValueaPopupMenu(0).Name = "CommandURL"
aPopupMenu(0).Value = CommandId
aPopupMenu(1).Name = "Label"
aPopupMenu(1).Value = Label
aPopupMenu(2).Name = "Type"
aPopupMenu(2).Value = 0
aPopupMenu(3).Name = "ItemDescriptorContainer"
aPopupMenu(3).Value = Factory.createInstanceWithContext( GetDefaultContext() )
CreatePopupMenu = aPopupMenu() End Function
Sub Test MsgBox "Test" End Sub
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
