I have created a windows service that is supposed to read the MS Excel file and store the data in the memory. Unfortunately before I could go on depper into my code I am already having difficulties accessing MS Excel file. In OnStart event when the function, ReadExcelFile, is called, the windows service will fail to start up. In windows application, similar function is coded but it's working fine.
Could anyone please advise what has gone wrong in my windows service code? Is there any built-in function or library that I am not permitted to use in services windows? Thank you.
The following is the code of the function in my windows service program:
--------------------------------------------------------------------------
Public Shared Function ReadExcelFile() As String
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Workbook
Dim xlWorkSheet As Worksheet
xlApp = New ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("C:\b.xls")
' ############# The area of difference ################
'For Each xlWorkSheet In xlWorkBook.Worksheets
' MessageBox.Show(xlWorkSheet.Name)
'Next
' #############################################
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Return Nothing
End Function
Private Shared Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
' Garbage collector
GC.Collect()
End Try
End Sub
####################################################
This is the version of the function used in windows application and it's proved to be working fine. The only difference is the For loop section that displays the sheets' names in message box.
---------------------------------------------------------------
Public Shared Function ReadExcelFile() As String
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Workbook
Dim xlWorkSheet As Worksheet
xlApp = New ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("C:\b.xls")
' ############# The area of difference ################
For Each xlWorkSheet In xlWorkBook.Worksheets
MessageBox.Show(xlWorkSheet.Name)
Next
' #############################################
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Return Nothing
End Function
Could anyone please advise what has gone wrong in my windows service code? Is there any built-in function or library that I am not permitted to use in services windows? Thank you.
The following is the code of the function in my windows service program:
--------------------------------------------------------------------------
Public Shared Function ReadExcelFile() As String
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Workbook
Dim xlWorkSheet As Worksheet
xlApp = New ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("C:\b.xls")
' ############# The area of difference ################
'For Each xlWorkSheet In xlWorkBook.Worksheets
' MessageBox.Show(xlWorkSheet.Name)
'Next
' #############################################
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Return Nothing
End Function
Private Shared Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
' Garbage collector
GC.Collect()
End Try
End Sub
####################################################
This is the version of the function used in windows application and it's proved to be working fine. The only difference is the For loop section that displays the sheets' names in message box.
---------------------------------------------------------------
Public Shared Function ReadExcelFile() As String
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Workbook
Dim xlWorkSheet As Worksheet
xlApp = New ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("C:\b.xls")
' ############# The area of difference ################
For Each xlWorkSheet In xlWorkBook.Worksheets
MessageBox.Show(xlWorkSheet.Name)
Next
' #############################################
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Return Nothing
End Function