[Solved] Macro to open toolbar i just created

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
User avatar
alf50
Posts: 131
Joined: Sun Jun 13, 2010 2:55 pm

[Solved] Macro to open toolbar i just created

Post by alf50 »

How do I write a Macro to Open a Toolbar I just created?

I found some Macro Code to do this, but it does not show the format of the "sURL" variable that points to the Toolbar?

The name of the ToolBar is "Controls". It is located in my "Timer.ods" Application, which is sitting in my Documents Folder, but when I set the sURL to "Controls", or "Timer.ods.Toolbars.Controls" Nothing happens. What am I doing wrong?

The Macro is shown below:

Code: Select all

Sub ToggleToolbarVisible(oDoc, sURL, prepinac) 
  Dim oLayout 
  oLayout = oDoc.CurrentController.getFrame().LayoutManager 
  Select Case prepinac 
    Case 0  rem --- hide toolbar 
      oLayout.hideElement(sURL) 
    Case 1  rem --- show toolbar 
      oLayout.showElement(sURL) 
    Case 2  rem --- switch toolbar 
      If oLayout.isElementVisible(sURL) Then 
        oLayout.hideElement(sURL) 
      Else 
        oLayout.showElement(sURL) 
      End If 
  End Select 
End Sub
Last edited by MrProgrammer on Mon May 05, 2025 7:12 pm, edited 2 times in total.
Reason: Lock ancient topic
OpenOffice 4.1.14 on Mac Catalina(10.15.7), RasPi4B (TwisterOS-8/2023update) & MS Wnds10
User avatar
kingfisher
Volunteer
Posts: 2127
Joined: Tue Nov 20, 2007 10:53 am

Re: Macro to Open a Toolbar I just created?

Post by kingfisher »

I use an url like this:

Code: Select all

sUrl = "private:resource/toolbar/standardbar"
If the tool bar is not visible, you may have to create it before trying to make it visible.
Apache OpenOffice 4.1.12 on Linux
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Macro to Open a Toolbar I just created?

Post by hanya »

User created toolbar has randomized internal resource id and it should be specified to do something with it.
It can be taken from UI settings stored in the document as follows:

Code: Select all

Sub ToolbarTest
  oDoc = ThisComponent
  sResId = FindToolbar(oDoc, "Controls")
  if not isnull(sResId) then
    oManager = oDoc.getCurrentController().getFrame().LayoutManager
    oManager.createElement(sResId)
    oManager.showElement(sResId)
  end if
End Sub

Function FindToolbar(oDoc, sName As String)
  oManager = oDoc.getUIConfigurationManager()
  aElements = oManager.getUIElementsInfo(_
       com.sun.star.ui.UIElementType.TOOLBAR)
  itemset = nothing
  for i = 0 to ubound(aElements) step 1
    a = aElements(i)
    if GetPropertyValueByName(a, "UIName") = sName then
      itemset = a
      exit for
    end if
  next
  if not isnull(itemset) then
    FindToolbar = GetPropertyValueByName(itemset, "ResourceURL")
  else
    FindToolbar = nothing
  end if
End Function

Function GetPropertyValueByName(aElements, sName) As Variant
  found = nothing
  for i = 0 to ubound(aElements) step 1
    if aElements(i).Name = sName then
      found = aElements(i).Value
    end if
  next
  GetPropertyValueByName = found
End Function
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Locked