[Solved] A macro to insert an item into a combobox list

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
martius
Posts: 60
Joined: Sat Feb 05, 2011 9:41 pm
Location: São Paulo, Brasil

[Solved] A macro to insert an item into a combobox list

Post by martius »

I have this macro to insert an item into the List Entries of a combobox in Calc:

Code: Select all

Sub insertItem
	Dim oSheet as object, oMyCombo as object

	oSheet = ThisComponent.CurrentController.ActiveSheet
        oMyCombo = oSheet.DrawPage.Forms.getByName("MyForm").getByName("MyComboBox")

	 	NewItem$ = "test"
			with oMyCombo
				.insertItemText(.ItemCount, NewItem)
			End with
End Sub

The problem is that, after I close the Calc file, when I open the file again, the item is not there in the list entries anymore.
How can I insert an item in the list entries that no disappears after I close the calc file?
I do not want to use a "source cell range", and the combobox was inserted directly in the sheet, not in a form.
Thanks! :D
Last edited by Hagar Delest on Thu Feb 23, 2012 9:23 am, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 6.2.8.2 (x64), Windows 10 Home
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: A macro to insert an item into a combobox list

Post by JohnSUN-Pensioner »

Maybe something like that?
Attachments
I don't want to use a "source cell range" too
I don't want to use a "source cell range" too
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: A macro to insert an item into a combobox list

Post by Charlie Young »

I'd been looking at this, and though apparently JohnSUN did it somehow, my explorations turned up that the Control.ListSourceType property of a combobox cannot be set to VALUELIST, and so must be a table, query, or cellrange. A regular listbox allows it, but that probably doesn't allow the desired funcionalty.

:?: :?: :?:
Apache OpenOffice 4.1.1
Windows XP
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: A macro to insert an item into a combobox list

Post by hanya »

Code: Select all

         with oMyCombo
            .StringItemList = Array(NewItem)
         End with
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
User avatar
Villeroy
Volunteer
Posts: 31355
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: A macro to insert an item into a combobox list

Post by Villeroy »

The combo box model is the blueprint of the control. It is roughly the same collection of properties you see in the properties dialog. The blueprint gets saved to disk.

Code: Select all

oCM = oMyCombo.getModel()
oCM.StringItemList = Array(All_Items)
 Edit: Sorry, the above code applies to a box on a dialog. A form control does not expose the strict separation of model and view. Try Hanya's snippet. 
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
martius
Posts: 60
Joined: Sat Feb 05, 2011 9:41 pm
Location: São Paulo, Brasil

Re: A macro to insert an item into a combobox list

Post by martius »

The situation is exactly like JohnSUN-Pensioner did, and the key to solve the problem is the combobox property "StringItemList", like Hanya's snippet and Villeroy's snippet.
So I could get this code below that is good to my needs:

Code: Select all

Sub insertItem
	Dim oSheet as object, oMyCombo as object
	Dim ListEntries() as string

	oSheet = ThisComponent.CurrentController.ActiveSheet
	oMyCombo = oSheet.DrawPage.Forms.getByName("MyForm").getByName("MyComboBox")
   
	NewItem$ = "Test"
	with oMyCombo
		ListEntries = .StringItemList
		NextItemIndex% = UBound(ListEntries) + 1
		Redim Preserve ListEntries(NextItemIndex)
		ListEntries(NextItemIndex) = NewItem
		.StringItemList = ListEntries()
	End with
End Sub
Thank you so much, everybody! :D :super:
LibreOffice 6.2.8.2 (x64), Windows 10 Home
Post Reply