Create listbox with code but can't select any items in list box

O

OssieMac

I actually posted this in a previous thread about a different issue that Dave
Peterson answered but because it is really an entirely different question I
thought it might be advisable to re-post in a separate thread.

Refer to my code example below with comments.

I create a list box on a worksheet using code but I am unable to then select
any of the items in the list box unless I manually turn Design Mode On then
Off again.

Therefore I thought that a work around would be to turn Design Mode On then
Off again with code. However, I can turn it On with code but have not been
successful in turning it Off.

I will very much appeciate any help I can get to overcome this problem.

Private Sub CommandButton2_Click()
Dim lListBox As Double
Dim tListBox As Double
Dim hListBox As Double
Dim wListBox As Double
Dim lstBox As OLEObject
Dim i As Long

With Me
lListBox = .Cells(2, 2).Left
tListBox = .Cells(2, 2).Top
wListBox = 100
hListBox = 150

Set lstBox = .OLEObjects.Add _
(ClassType:="Forms.ListBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=lListBox, _
Top:=tListBox, _
Width:=wListBox, _
Height:=hListBox)

With lstBox.Object
For i = 1 To 10
.AddItem "Test" & i
Next i
.MultiSelect = fmMultiSelectMulti
End With

End With

Set lstBox = Nothing

'**********************************
'Tested the following code with minimal success
'Following line turns Design Mode ON in xl2007
Application.CommandBars.ExecuteMso ("DesignMode")

'Following line turns Design Mode ON in xl2002
'(Also works in xl2007)
'Application.CommandBars.FindControl(ID:=1605).Execute

'Following lines turn Design Mode OFF in xl2002
'but will only work if run in a separate sub
'after this sub has finished.

'With CommandBars("Exit Design Mode").Controls(1)
' If .State = msoButtonDown Then .Execute
'End With
'*************************************

End Sub
 
B

Bob Phillips

Put this in a standard code module

Public Sub TurnDesignModeOff()
With CommandBars("Exit Design Mode").Controls(1)
If .State = msoButtonDown Then .Execute
End With
End Sub

and change the button code to

Private Sub CommandButton2_Click()
Dim lListBox As Double
Dim tListBox As Double
Dim hListBox As Double
Dim wListBox As Double
Dim lstBox As OLEObject
Dim i As Long

With Me
lListBox = .Cells(2, 2).Left
tListBox = .Cells(2, 2).Top
wListBox = 100
hListBox = 150

Set lstBox = .OLEObjects.Add _
(ClassType:="Forms.ListBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=lListBox, _
Top:=tListBox, _
Width:=wListBox, _
Height:=hListBox)

With lstBox.Object
For i = 1 To 10
.AddItem "Test" & i
Next i
.MultiSelect = fmMultiSelectMulti
End With

End With

Set lstBox = Nothing

Application.OnTime Now() + TimeSerial(0, 0, 1), "TurnDesignModeOff"

Application.CommandBars.FindControl(ID:=1605).Execute
End Sub

HTH

Bob
 
O

OssieMac

Hi Bob,

Excellent! So simple now; I never gave it a thought to use the timer to run
the second sub to turn it off. Thank you for your efforts.
 
V

Varne

Hi!

Just a trick;

On Error Resume Next
Application.CommandBars.ExecuteMso ("Something")
On Error GoTo 0

That will turn off the design mode!

M Varnendra
 
O

OssieMac

Thanks for your effort Varne. It works in conjunction with Bob Phillips
answer. However, I think that p45cal's answer is the one to use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top