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
Solved

Exception while adding a formula to excel via csharp Interop

Posted on 2011-09-06
1
457 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

839 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