Code:
Imports System.IO
Imports Microsoft.Office.Interop.Excel
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Threading
Imports System.Globalization
'------------------------------
'Usage
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim result As String() = testFind("C:\myXlFile.xls", TextBox1.Text, "A")
MsgBox(result(0) & vbLf & result(1))
End Sub
Public Function testFind(ByVal xlfileName As String, ByVal strToSearh As String, ByVal colName As String) As String()
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim result() As String = New String(1) {}
Dim xlApp As Excel.Application = Nothing
Dim xlBooks As Excel.Workbooks = Nothing
Dim xlBook As Excel.Workbook = Nothing
Dim xlSheet As Excel.Worksheet = Nothing
Dim xlRange As Excel.Range = Nothing
Dim xlFindRange As Excel.Range = Nothing
Dim missing As Object = Type.Missing
Try
xlApp = New Excel.Application()
xlApp.Visible = True
xlApp.DisplayAlerts = False
xlApp.UserControl = True
xlBooks = DirectCast(xlApp.Workbooks, Excel.Workbooks)
'Change full path of Excel file here:
xlBook = DirectCast(xlBooks.Open(xlfileName, True, False, missing, "", missing, _
False, missing, missing, True, missing, missing, missing, missing, missing), Excel.Workbook)
'get first sheei in the workbook
xlSheet = DirectCast(xlBook.Worksheets.Item(1), Excel.Worksheet)
'get used range of certain sheet
xlRange = DirectCast(xlSheet.UsedRange, Excel.Range)
'' get colunmn "A" of this range
xlRange = DirectCast(xlRange.Columns(colName), Excel.Range)
xlFindRange = DirectCast(FindInColumn(xlRange, strToSearh, Nothing, _
Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, _
Microsoft.Office.Interop.Excel.XlLookAt.xlWhole), Excel.Range)
If xlFindRange Is Nothing Then
MsgBox("Referenced value was not found")
End If
result(0) = xlFindRange.Value.ToString()
result(1) = xlFindRange.Offset(0, 1).Value.ToString()
DirectCast(xlBook, Excel._Workbook).Close(True, missing, missing) ''<-- see here
releaseObject(xlRange)
releaseObject(xlSheet)
releaseObject(xlBook)
releaseObject(xlBooks)
Catch ex As Exception
MsgBox("Error:" & vbLf & ex.Message & "Trace: " & vbLf & ex.StackTrace)
Finally
xlApp.Quit()
releaseObject(xlApp)
End Try
Return result
End Function
' autor unknown
Function FindInColumn(ByVal rng As Excel.Range, _
Optional ByVal what As String = "*", _
Optional ByVal after As Excel.Range = Nothing, _
Optional ByVal lookin As Microsoft.Office.Interop.Excel.XlFindLookIn = Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, _
Optional ByVal lookat As Microsoft.Office.Interop.Excel.XlLookAt = Microsoft.Office.Interop.Excel.XlLookAt.xlWhole, _
Optional ByVal order As Microsoft.Office.Interop.Excel.XlSearchOrder = Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, _
Optional ByVal direction As Microsoft.Office.Interop.Excel.XlSearchDirection = Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious, _
Optional ByVal matchcase As Boolean = False) As Excel.Range
If after Is Nothing Then
after = rng.Cells(1, 1)
End If
Return rng.Find(What:=what, _
After:=after, _
LookIn:=lookin, _
LookAt:=lookat, _
SearchOrder:=order, _
SearchDirection:=direction, _
MatchCase:=matchcase)
End Function
Public Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
obj = Nothing
Catch ex As System.Exception
System.Diagnostics.Debug.Print(ex.ToString())
obj = Nothing
Finally
GC.Collect()
End Try
End Sub