1

How to open an excel workbook from combobox?

I am using following code to populate combobox with excel filenames,

Dim files() As String
files = Directory.GetFiles("C:\Files\Folder", "*.xlsx", SearchOption.AllDirectories)
For Each FileName As String In files
ComboBox1.Items.Add(FileName.Substring(FileName.LastIndexOf("\") + 1, FileName.Length -     FileName.LastIndexOf("\") - 1))
Next

But I do not have any idea about how to open the selected file.

1
  • Do you want to just open the Excel file or do you want to further interact with it? Dec 22, 2012 at 7:05

3 Answers 3

2
Imports System
Imports System.IO
Imports System.Collections

Public Class Form1

Private Const TargetDir = "C:\Files\Folder"
Private FullFileList As List(Of String)


Private Sub ProcessFile(ByVal fn As String)
    If Path.GetExtension(fn).ToLower = ".xlsx" Then
        FullFileList.Add(fn)
        ComboBox1.Items.Add(Path.GetFileName(fn))
    End If
End Sub

Private Sub ProcessDirectory(ByVal targetDirectory As String)
    Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
    Dim fileName As String
    For Each fileName In fileEntries
        ProcessFile(fileName)
    Next fileName
    Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
    Dim subdirectory As String
    For Each subdirectory In subdirectoryEntries
        ProcessDirectory(subdirectory)
    Next subdirectory
End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    FullFileList = New List(Of String)
    ProcessDirectory(TargetDir)
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    If ComboBox1.SelectedIndex >= 0 Then
        Dim prc As New System.Diagnostics.Process
        Dim psi As New System.Diagnostics.ProcessStartInfo(FullFileList(ComboBox1.SelectedIndex))
        psi.UseShellExecute = True
        psi.WindowStyle = ProcessWindowStyle.Normal
        prc.StartInfo = psi
        prc.Start()
    End If
End Sub

End Class
3
  • just one small query, under "GetFile" if we are searching in all sub directories then how do we open selected file?
    – mrn
    Dec 22, 2012 at 7:27
  • In that case you'll need to keep a seperate list of full paths, I'll update my answer. Give me a moment and I'll do a few other mods to how I'd normally do it.
    – PeterJ
    Dec 22, 2012 at 7:29
  • Just updated - actually your original code didn't work as I thought it would have so used an example from MSDN to recurse directories along with my original code to launch Excel.
    – PeterJ
    Dec 22, 2012 at 8:12
0

You'll need to handle an event to react to the user's selection: either a change to the selection in the ComboBox or (better) a Button click. In your event handler you can retrieve the filename from the ComboBox:

string filename = ComboBox1.SelectedItem.ToString()

Once you have the filename, you can open the file, although how you do this will depend on what you want to do with it. This answer has some information on opening Excel files in VB.NET.

0

Try using

Process.start("excel "+ComboBox1.SelectedItem.ToString());

on buttonclick event

1
  • Thank you for your reply. It gives an error "can not find file specified". Which is understandable as I am adding only filenames to combobox. But when I use "GetFile" it gets the filepath. Is there any way to show only filename and while opening file use entire file path?
    – mrn
    Dec 22, 2012 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.