vbamacrosopenoffice.orgopenoffice-calcopenoffice-basic

OpenOffice BASIC how to insert checkbox in sheet


I'm using OpenOffice Calc. And I am writing macro's in OpenOffice BASIC. I need the right code to insert a checkbox in the sheet.

I now have

Dim Doc as Object
Doc = ThisComponent
Dim cbName As Object
cbName = "checkbox_name"

Dim oCheckBoxModel as Object

// dlg is a dialog, (don't know how to create a checkbox else)
oCheckBoxModel = dlg.getmodel().createInstance( "com.sun.star.awt.UnoControlCheckBoxModel" )
oCheckBoxModel.PositionX = 100
oCheckBoxModel.PositionY = 100
oCheckBoxModel.Width = 50
oCheckBoxModel.Height = 30
oCheckBoxModel.Label = id
oCheckBoxModel.Name = cbName
oCheckBoxModel.Enabled = True
oCheckBoxModel.TabIndex = 1
Doc.Sheets().insertByName( cbName, oCheckBoxModel ) // This line is totally wrong, but I hope it's clear what I want to do

So I want to create a checkbox, and then insert it into the sheet. (In a specific cell, or just by setting a X and Y position). I searched on the internet, but I only find information about inserting controls into a dialog, not into a sheet


Solution

  • To create check boxes manually, see here. To create check boxes dynamically:

    Sub CreateCheckbox
      oDoc = ThisComponent
      oSheet = oDoc.Sheets.getByIndex(0)
      oDrawPage = oSheet.DrawPage  'Was oDrawPage = oDoc.getDrawPage()
      oCheckboxModel = AddNewCheckbox("Checkbox_1", "Check this box", oDoc, oDrawPage)
    End Sub
    
    Function AddNewCheckbox(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.CheckBox")
      oButtonModel.Name = sName
      oButtonModel.Label = sLabel
    
      oControlShape.setControl(oButtonModel)
      oDrawPage.add(oControlShape)
    
      AddNewCheckbox = oButtonModel
    End Function
    

    This code was adapted from https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=46391.