How do you get the selected text from a ComboBox?

I am trying to get the selectedText from a ComboBox. This is the code I am trying:

Function GetSelectedText(oDialog As Object, comboBoxName As String) As String
    Dim oComboBox As Object
    Dim selectedText As String

    ' Get the ComboBox control by name from the dialog
    oComboBox = oDialog.getControl(comboBoxName)

    ' Check if the ComboBox control exists
    If Not IsEmpty(oComboBox) Then
        ' Retrieve the text of the selected item
        selectedText = oComboBox.Text
        MsgBox "selectedText = """ & selectedText &""""
    Else
        MsgBox "ComboBox control '" & comboBoxName & "' not found on the dialog."
    End If

    ' Return the selected text
    GetSelectedText = selectedText
End Function

I always get: "selectedText = " even though the oComboBox has selected text.
The documentation I have seen hasn’t been helpful.
Help would be appreciated.

is the combo box a data field in a table or view ? > if so need something like:

Rem Get the main Form
oForm = ThisComponent.getDrawPage().getForms().getByName(“Form”)
Rem Get the SubForm
oSubForm = oForm.getByName(“SubForm”)
Rem Get the Table control
oTable = oSubForm.getByName(“Table Control 1”)
Rem Get the value selected in ‘type’
oColumn = oTable.getByName(“IMAGE_TYPE”) REM that is a column name
STUFF = oColumn.SelectedValue
msgbox “stuff” & STUFF

to get the current values in control not necessarily linked to a data table you need something like:

oTextField = oStartDoc.drawpage.forms.FORM1.getControl(“Control 17”)
STUFF = oTextField.getSelectedText()
MsgBox “stuff” & STUFF

hope that helps :slight_smile:

I tried modifying my code from:
selectedText = oComboBox.Text
to:
selectedText = oComboBox.getSelectedText()

and it still resulted in “”

To fill the ComboBox initially, I used the following Code:

    oComboBox.addItem("ALL ERRORS", 0)
    oComboBox.Text = "ALL ERRORS" ' Select the first item
    Dim selectedText As String
    selectedText = oComboBox.getSelectedText()
    MsgBox "selectedText = '" & selectedText & "'"

and the ComboBox shows the text “ALL ERRORS”
yet the initial selectedText = ‘’

https://www.openoffice.org/api/docs/common/ref/com/sun/star/awt/XTextComponent.html#getSelection

1 Like

yes i see ---- try ’ ’ ?

I don’t understand this. It is for Apache OpenOffice 4.1.15

I tried:

  • oComboBox.Text
  • oComboBox.getText()
  • oComboBox.getSelectedText()

None of those gave me the text that was visible in the oComboBox

Same API (mostly) for LibreOffice: LibreOffice: XTextComponent Interface Reference

XTextComponent_Selection.odt (14.8 KB)

Let’s do it together.
Open the attached file and run the ShowDialog macro.
Next, enter the text ABCD into the ComboBox, select BC and press the Show button.
There should be a message with the text BC.
TestCombo.ods (9.9 KB)

1 Like

TestCombo2.ods (11.1 KB)

A combo box is just a text box with auto-complete from a given list. Most VBA heroes want a list box rather than a combo box.
For a combo box the position of the selected list item is only availlable when the item has been clicked. The selected text of a combo box can be anything because you can enter anything beyond list items.

1 Like

Yes, that’s the source of the confusion.
When the value of the MS Office ComboBox object style property is fmStyleDropDownList (2) then

The ComboBox behaves as a list box. The user must choose a value from the list.

1 Like