Listbox click event

molesy01

Board Regular
Joined
Dec 23, 2012
Messages
61
Hi, hope someone can help with this. I have listbox1 that has multiple items. I would like to select an item by double clicking the selected item and open a file. I have the path of where the file is so just need the code when double clicking the selection.
thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Code:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Workbooks.Open ("[COLOR=#ff0000]C:\Folder\SubFolder\[/COLOR]" & [COLOR=#0000cd]ListBox1.Value[/COLOR] & "[COLOR=#006400].xlsx[/COLOR]")
End Sub

1. Path to file ending with path separator
2. Value from listbox ,
3. File extension prefixed by .
 
Last edited:
Upvote 0
Hi, hope someone can help with this. I have listbox1 that has multiple items. I would like to select an item by double clicking the selected item and open a file. I have the path of where the file is so just need the code when double clicking the selection.
thanks

Hi Thanks for your reply but i cannot get it to work.

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Workbooks.Open ("C:\Users\Stephen\Dropbox\GuidanceNotesMastercustomUIcode" & ListBox1.Value & ".docx")
End Sub
 
Upvote 0
Include the path separator at end of file path
 
Upvote 0
It would have been helpful if you mentioned that you wanted to open a WORD document

Code:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    With CreateObject("word.Application")
        .documents.Open "C:[SIZE=4][COLOR=#ff0000]\[/COLOR][/SIZE]Folder[SIZE=4][COLOR=#ff0000]\[/COLOR][/SIZE]SubFolder[SIZE=4][COLOR=#ff0000]\[/COLOR][/SIZE]" & ListBox1.Value & ".docx"
        .Visible = True
        .Activate
    End With
End Sub

Don't forget to include path separator before the file name

To allow for both BOTH Excel and Word files then I suggest
- file name including extension are selected in the Listbox
- tell VBA to use Workbook.Open method if .XLSX
- tell VBA to use Documents.Open method if .DOCX

Above assumes that the ListBox is in Excel
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top