Solved

Exception while adding a formula to excel via csharp Interop

Posted on 2011-09-06
1
454 Views
Last Modified: 2012-05-12
Hi,

i'm trying to add an if-condition to an specific cell in excel via csharp interop api.
But wenn i run the code, i got following exception:
"Exception from HRESULT: 0x800A03EC"
- without the if-condition, it would run perfectly
- i also implemented the cultureinfo-workaround described at the microsoft-url you can see at the sourcecode
- i use following Assembly: C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll

Any ideas, what's wrong?

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
...
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = false;
//http://support.microsoft.com/default.aspx?scid=kb;en-us;320369 workaround 
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
...
Excel._Workbook wb = (Excel._Workbook)(xlApp.Workbooks.Add(Missing.Value));	
Excel._Worksheet ws = (Excel._Worksheet)wb.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
...
Excel.Range formularRange = ws.get_Range("D2","D2");
//working code: 
formularRange.Formula = "=C2/B2";
//code throwing exception
formularRange.Formula = "=IF(B2=0;0;C2/B2)";

Open in new window

0
Comment
Question by:horti
1 Comment
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 36487177
Try using:

formularRange.Formula = "=IF(B2=0,0,C2/B2)";
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now