?
Solved

Exception while adding a formula to excel via csharp Interop

Posted on 2011-09-06
1
Medium Priority
?
461 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

770 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