Question Start up issue when accessing MS Excel file

totziens

New member
Joined
Jun 7, 2010
Messages
2
Programming Experience
Beginner
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
 
Thank you very much. Now I am concern because this is the main implementation at the server for the system I am developing.

I am very new to VB .Net as well as windows service. The site mentions about third party solution or develop the alternative solution on our own, but do not elaborate further. Does anyone familiar with the alternative solution? How to achieve the alternative solution?
 
Back
Top