Solved

Exception while adding a formula to excel via csharp Interop

Posted on 2011-09-06
1
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

726 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