Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Exception while adding a formula to excel via csharp Interop

Posted on 2011-09-06
1
Medium Priority
?
465 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 2000 total points
ID: 36487177
Try using:

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

Featured Post

Technology Partners: 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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

609 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