Hello,
I am a long time user of VBA but like the idea of converting to OO. I currently have a project in VBA where I create multiplesheets from code and install buttons on several of them.
I've managed to duplicate all except creating the buttons in OO but can't figure a way to create and position the buttons on a sheet and also then subsequently add an "onclick" assignment to a macro already contained the in the file.
Any help would be appreciated,
Thanks,
[Solved] Creating Buttons and Assigning Code from Macro
[Solved] Creating Buttons and Assigning Code from Macro
Last edited by Hagar Delest on Mon Feb 15, 2010 9:24 am, edited 1 time in total.
Reason: tagged [Solved].
Reason: tagged [Solved].
OpenOffice 3.1 on Windows XP Pro
Re: Creating Buttons and Assigning Code from Macro
Does this thread help? http://www.oooforum.org/forum/viewtopic ... ert+button
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Creating Buttons and Assigning Code from Macro
Yes, it helped greatly. Thank you. Now I just need to figure out a way to be able to assign the button a macro via code. I've read a few threads on it but can't seem to find a way to assign it at all. I would like to be able to assign one right after I've created it.
OpenOffice 3.1 on Windows XP Pro
Re: Creating Buttons and Assigning Code from Macro
I copied this code from Hanya's post in this thread http://user.services.openoffice.org/en/ ... 20&t=25554 and made some very minor modifications. I put the code in Module1 of the Standard library of a Calc document. The code puts a button on the second sheet of the Calc document and assigns it the sub routine ButtonPushEvent stored in the same module. You can modify the macro URL to point to whatever macro you want. If the called macro is store in My Macros, the location should be edited back to "application"
Code: Select all
Sub CreateButton
oDoc = ThisComponent
oSheet = oDoc.Sheets.getByIndex(1)
oDrawPage = oSheet.DrawPage 'Was oDrawPage = oDoc.getDrawPage()
sScriptURL = "vnd.sun.star.script:Standard.Module1.ButtonPushEvent?language=Basic&location=document" 'Was location=application
oButtonModel = AddNewButton("Button_1", "Button_1", oDoc, oDrawPage)
oForm = oDrawPage.getForms().getByIndex(0)
' find index inside the form container
nIndex = GetIndex(oButtonModel, oForm)
AssignAction(nIndex, sScriptURL, oForm)
End Sub
' assign sScriptURL event as css.awt.XActionListener::actionPerformed.
' event is assigned to the control described by the nIndex in the oForm container
Sub AssignAction(nIndex As Integer, sScriptURL As String, oForm As Object)
aEvent = CreateUnoStruct("com.sun.star.script.ScriptEventDescriptor")
With aEvent
.AddListenerParam = ""
.EventMethod = "actionPerformed"
.ListenerType = "XActionListener"
.ScriptCode = sScriptURL
.ScriptType = "Script"
End With
oForm.registerScriptEvent(nIndex, aEvent)
End Sub
Function AddNewButton(sName As String, sLabel As String, oDoc As Object, oDrawPage As Object) As Object
oControlShape = oDoc.createInstance("com.sun.star.drawing.ControlShape")
aPoint = CreateUnoStruct("com.sun.star.awt.Point")
aSize = CreateUnoStruct("com.sun.star.awt.Size")
aPoint.X = 1000
aPoint.Y = 1000
aSize.Width = 3000
aSize.Height = 1000
oControlShape.setPosition(aPoint)
oControlShape.setSize(aSize)
oButtonModel = CreateUnoService("com.sun.star.form.component.CommandButton")
oButtonModel.Name = sName
oButtonModel.Label = sLabel
oControlShape.setControl(oButtonModel)
oDrawPage.add(oControlShape)
AddNewButton = oButtonModel
End Function
Function GetIndex(oControl As Object, oForm As Object) As Integer
Dim nIndex As Integer
nIndex = -1
For i = 0 To oForm.getCount() - 1 step 1
If EqualUnoObjects(oControl, oForm.getByIndex(i)) Then
nIndex = i
Exit For
End If
Next
GetIndex = nIndex
End Function
Sub ButtonPushEvent(ev as com.sun.star.awt.ActionEvent)
Print "HERE"
'msgbox ev.Source.Model.Name
End Sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Creating Buttons and Assigning Code from Macro
THANK YOU!!!!!!!!!
It took me quite a while to adapt it, but I got it. (I'm adding multiple buttons with different scripts for each).
FJCC, THANK YOU VERY MUCH
It took me quite a while to adapt it, but I got it. (I'm adding multiple buttons with different scripts for each).
FJCC, THANK YOU VERY MUCH
OpenOffice 3.1 on Windows XP Pro
Re: Creating Buttons and Assigning Code from Macro
In Case Any are interested, Here is the Script with the Modifications I added/made.
Code: Select all
REM ***** BASIC *****
Sub Main
MyXPos=1000
MyYPos=1000
MyHeight=1000
MyWidth=3000
MyButtonNum=1
MySheetNum=0
MyCaption="First Button Text"
MyScriptLoc="Standard.Module1.FirstPushButton"
CreateButton(MyXPos,MyYPos,MyHeight,MyWidth,MyButtonNum,MyCaption,MyScriptLoc,MySheetNum)
MyXPos=1000
MyYPos=1000
MyHeight=1000
MyWidth=3000
MyButtonNum=2
MySheetNum=1
MyCaption="Second Button Text"
MyScriptLoc="Standard.Module1.SecondPushButton"
CreateButton(MyXPos,MyYPos,MyHeight,MyWidth,MyButtonNum,MyCaption,MyScriptLoc,MySheetNum)
End Sub
Sub CreateButton(ButtonXPos as Integer, ButtonYPos as Integer, ButtonHeight as Integer, ButtonWidth as Integer, ButtonNum as Integer, ButtonText as String, ButtonScriptStr as String, ButtonSheetNum as Integer)
oDoc = ThisComponent
oSheet = oDoc.Sheets.getByIndex(ButtonSheetNum)
oDrawPage = oSheet.DrawPage 'Was oDrawPage = oDoc.getDrawPage()
sScriptURL = "vnd.sun.star.script:" & ButtonScriptStr & "?language=Basic&location=document" 'Was location=application
oButtonModel = AddNewButton("Button_"&Str(ButtonNum), ButtonText, oDoc, oDrawPage,ButtonXPos,ButtonYPos,ButtonHeight,ButtonWidth)
oForm = oDrawPage.getForms().getByIndex(0)
' find index inside the form container
nIndex = GetIndex(oButtonModel, oForm)
AssignAction(nIndex, sScriptURL, oForm)
End Sub
' assign sScriptURL event as css.awt.XActionListener::actionPerformed.
' event is assigned to the control described by the nIndex in the oForm container
Sub AssignAction(nIndex As Integer, sScriptURL As String, oForm As Object)
aEvent = CreateUnoStruct("com.sun.star.script.ScriptEventDescriptor")
With aEvent
.AddListenerParam = ""
.EventMethod = "actionPerformed"
.ListenerType = "XActionListener"
.ScriptCode = sScriptURL
.ScriptType = "Script"
End With
oForm.registerScriptEvent(nIndex, aEvent)
End Sub
Function AddNewButton(sName As String, sLabel As String, oDoc As Object, oDrawPage As Object, XPos as Integer, YPos as Integer, Height as Integer, Width as Integer) As Object
oControlShape = oDoc.createInstance("com.sun.star.drawing.ControlShape")
aPoint = CreateUnoStruct("com.sun.star.awt.Point")
aSize = CreateUnoStruct("com.sun.star.awt.Size")
aPoint.X = XPos
aPoint.Y = YPos
aSize.Width = Width
aSize.Height = Height
oControlShape.setPosition(aPoint)
oControlShape.setSize(aSize)
oButtonModel = CreateUnoService("com.sun.star.form.component.CommandButton")
oButtonModel.Name = sName
oButtonModel.Label = sLabel
oControlShape.setControl(oButtonModel)
oDrawPage.add(oControlShape)
AddNewButton = oButtonModel
End Function
Function GetIndex(oControl As Object, oForm As Object) As Integer
Dim nIndex As Integer
nIndex = -1
For i = 0 To oForm.getCount() - 1 step 1
If EqualUnoObjects(oControl, oForm.getByIndex(i)) Then
nIndex = i
Exit For
End If
Next
GetIndex = nIndex
End Function
Sub FirstPushButton
MsgBox("This is the First Push Button")
End Sub
Sub SecondPushButton
MsgBox("This is the Second Push Button")
End Sub
OpenOffice 3.1 on Windows XP Pro