Error when trying to create an Excel workbook

I have an SQL query result that I need to put into an Excel workbook for emailing purposes. I have Excel 2007 installed and the Interop 14.0 referenced in my project. The problem shows when I make the call that follows:

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

When the debugger hits this line of code I get the following error:

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005. Access is denied.

I tried configuring the DCOM in Windows on the machine the service is running on, but the error still shows. What do I need to change to allow the Excel workbook to be created correctly?
Abacus ITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Can you create an ApplicationClass() instead of Application()?
Can you run your app as an administrator (if you're on Vista / 7)?

Also, take a look at this example:
You might get away with using that class.
Abacus ITAuthor Commented:
The machine I am running on is Windows XP SP3.
I took a look at the link you provided, and my code is very similar to that setup already. I feel like there is some kind of permissions setting I am not setting before I make the call to create the Application.
In your XAML code, did you specify Style element?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

NorieAnalyst Assistant Commented:
Is this related to your previous question?
Are you using a 32-bit or 64-bit machine?
Abacus ITAuthor Commented:
Imnorie, yes this is related to the other question I posted. The machine I am running on is a 32-bit system.
NorieAnalyst Assistant Commented:
Do you mean the machine is 32-bit?
The reason I'm asking is because I recently ran into some trouble because I was using a 32-bit version of
something on a 64-bit machine.
As far as I knew none of the applications I was using was the 64-bit version but for some reason
the target CPU had been set to 64-bit.
Had to change some settings in Visual Studio to get things to work.
The only other thing I can think, off the top of my head, is the reference thing I mentioned in the other post.
Abacus ITAuthor Commented:
The machine is 32-bit. I'm pretty sure when I build my service, it is set to build for any CPU type in the project properties window. I will look back again at the references and see if something comes up.
NorieAnalyst Assistant Commented:
Well I didn't actually know there could be a problem with the CPU type until I encountered the problem I mentioned.
It was actually an ASP.NET problem rather than Excel mind you.
When using Excel and C# I was getting similar errors to what you describe.
I never found a comprhensive solution, and now I just make sure I use the same references
all the time - if they work of course.
I've no idea if it would help but can you post the problem code and info on the references etc.
If you can't post all the code, for confidentiality reasons say, perhaps an explanation of what the bits you don't
post are for will do.
Abacus ITAuthor Commented:
As far as the code goes, I am using the same code, with slight variations for my client, that you posed in the other post. I am using Interop.Excel version 14.0.
In the sample code I am providing, I get the error on line 5, the xlApp call to create the instance of Excel. I have Excel 2007 installed on the machine.
The error code I am getting looks as follows:

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

If I can get the call to Excel working, I know the rest of my code, which is simply filling in the data into the Excel sheet. What I also need to do, is save the sheet to a network location, which should be simple to do, and then I have code to send out an email with the created Excel workbook as an attachment.
Microsoft.Office.Interop.Excel.Application xlApp = null;
            Microsoft.Office.Interop.Excel.Workbook xlwb = null;
            Microsoft.Office.Interop.Excel.Worksheet xlsheet = null;
            Microsoft.Office.Interop.Excel.Range rng = null;

            xlApp = new Microsoft.Office.Interop.Excel.Application();

            xlwb = xlApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            xlsheet = xlwb.Worksheets.get_Item(1);

            xlApp.Visible = true;

            rng = xlsheet.get_Range("A1", "A1");

            foreach (DataColumn fldname in dt.Tables[0].Columns)
                rng.Value = fldname.ColumnName;
                rng = rng.get_Offset(0, 1);

            rng = xlsheet.get_Range("A2", "A2");

            foreach (DataRow rw in dt.Tables[0].Rows)
                foreach (DataColumn cl in dt.Tables[0].Columns)

                    rng.Value = rw[cl].ToString();
                    rng = rng.get_Offset(0, 1);


                rng = rng.get_Offset(1, -rng.Column + 1);

Open in new window

NorieAnalyst Assistant Commented:
Did you try version 12.0 of the Interop?
I've tried all the versions I've got and they all work fine.
Abacus ITAuthor Commented:
Let me try putting the Interop 12.0 in place of the 14.0 and see what I get.
Abacus ITAuthor Commented:
I tried changing the reference to 12.0 and still got the same error. Not sure if i mentioned before, but this code is running as part of a Windows service. I know sometimes code can work fine in a forms application, and not in a service, but that doesn't seem to be the issue here.
NorieAnalyst Assistant Commented:
Don't know to much about that sort of thing, but I certainly wouldn't dismiss it.
I actually found somebody else here that was having basically the same problem, the error message was
almost identical.
Think they actually ended up re-installing either VS or Excel, and that was after various other things.
eg uninstalling every version of Excel and re-installing just the one version.
Abacus ITAuthor Commented:
I might try doing that. Maybe upgrading it to Office 2010. But I don't want to have to make alot of changes to my code. Should just be a change in the reference back to 14.o Interop.
NorieAnalyst Assistant Commented:
Another thing I found was adding NETWORK SERVICES using Dcomcnfg, but I think that could have security implications.
By the way I found that just by searching on the error message, there were quite a few results.
Seems that other people have been having the same or similar problems.:)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Abacus ITAuthor Commented:
Adding the Network Service in DCOM did fix my issue and it is working as needed now. Still need to decide and get confirmation that we won't run into any glaring security issues, but it's doing what I need it to do. Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.