Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
Am new to VB.Net. I have created a windows service. Where my simple goal is to create an Excel file and save it to my local drive. I have used the following code.

VB
 Dim oExcel As Excel.Application
            Dim oBook As Excel.Workbook
            Dim oSheet As Excel.Worksheet
            oExcel = CreateObject("Excel.Application")
            oBook = oExcel.Workbooks.Add

'Start a new workbook in Excel.

            'Add data to cells of the first worksheet in the new workbook.
            oSheet = oBook.Worksheets(1)
            oSheet.Range("A1").Value = "Last Name"
            oSheet.Range("B1").Value = "First Name"
            oSheet.Range("A1:B1").Font.Bold = True
            oSheet.Range("A2").Value = "Doe"
            oSheet.Range("B2").Value = "John"

            oBook.SaveAs(strFileName)
            oSheet = Nothing
            oBook = Nothing
            oExcel.Quit()
            oExcel = Nothing
            GC.Collect()

Now this CreateObject throwing exception "Cannot create ActiveX component." I have added reference of Microsoft Excel & Office in my project.

Any idea what am doing wrong or what should be done? It is possible to create and save excel file from windows service right?

P.S - Am on windows 7 64 bit machine and using vs2010, office 2010.
Posted
Comments
CHill60 26-Feb-13 6:44am    
You should be using oExcel = new Excel.Application() not CreateObject
A Roychowdhury 26-Feb-13 7:11am    
Thanks for your reply CHill60..however this time I got the following exception.
"Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005."
CHill60 26-Feb-13 7:21am    
See solution below - you shouldn't be using a COM interface to excel so check how you added the references to your project

1 solution

It may be possible as long as Excel is on the box that the service is running on, although I've heard on StackExchange that interop doesn't work as a server application

Have a look at this article[^] from Microsoft
or a very good alternative approach [^] from our very own CodeProject which may be the very solution you need for a service
 
Share this answer
 
Comments
A Roychowdhury 26-Feb-13 7:15am    
The msdn article is referring to a console application. But am looking for a windows service to do the same.
CHill60 26-Feb-13 7:19am    
They tend to use Console applications for ease of demonstration - so they can use consolewriteline to show the working. The main principles are the same.
[Edit] - do you specifically need a "service" if you are saving to your local drive - would a winform application be appropriate?
A Roychowdhury 26-Feb-13 7:25am    
Actually I need a application which will automatically run,create and save some excel reports preodically. So, I figured out that windows service will be teh best solution to do so as it'll never need user intervention to start.
CHill60 26-Feb-13 7:29am    
Cool - just thought I'd check :-) I like to "start simple" and layer up the complexity so maybe having a console app running under a windows scheduler for starters might get you going
A Roychowdhury 26-Feb-13 7:35am    
So we can add scheduler to console application? that'll do. can you please share how to use that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900