Imports excel = Microsoft.Office.Interop.Excel
Imports forms = System.Windows.Forms
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Dim ci As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")
Dim xlapp As excel.Application
Dim xlworkbook As excel.Workbook
Dim dlg As New forms.OpenFileDialog
With dlg
.CheckFileExists = True
.CheckPathExists = True
.ShowDialog()
End With
If dlg.FileName <> Nothing Then
xlapp = New excel.Application
xlapp.Visible = False
Try
' This gives me an error because Offices language is set to English
' but my regional settings are Dutch. I don't have the language packs
' installed. More information here: http://support.microsoft.com/kb/320369
xlworkbook = New excel.Workbook
'xlworkbook.GetType().InvokeMember("Open", Reflection.BindingFlags.InvokeMethod, Nothing, xlapp, Nothing, ci)
xlworkbook = xlapp.Workbooks.Open(dlg.FileName)
For Each w As excel.Worksheet In xlworkbook.Worksheets
ListBox1.Items.Add(w.Name)
Next
releaseObject(xlapp)
releaseObject(xlworkbook)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class