Candy Chiu
Candy Chiu

Reputation: 6679

Emulate Open Excel in C#

I have a spreadsheet which is probably calling BDDE.EXE. When I open this spreadsheet in Excel, everything works fine. I can see values in cells whose formula starts with "=BDDE". However, when I open the same file using C#, Excel first displayed an Alert

Remote data not accesible.
To access this data Excel needs to start another application. ...
Start application 'BDDE.EXE'?

Then no matter what I click - Yes or No, the formulas are updated, all the values from a previous recalculation are lost.

I then tried forcing Excel not to recalculate by setting XlCalculation to Manual. Open stops working after this change, and threw an COMException (with no other information).

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT
: 0x800A03EC
at Microsoft.Office.Interop.Excel.ApplicationClass.set_Calculation(
XlCalculation RHS)

I got completely stuck. I can't recalculate because that would mess up all the numbers. I can't set XlCalcualtion to Manual as it throws Exception.

Any help is appreciated.

Upvotes: 1

Views: 848

Answers (2)

Gary McGill
Gary McGill

Reputation: 27536

You can't set the Calculation property without first opening the workbook. This sounds like Catch 22, but it's not actually the 'calculation' you're trying to prevent here - you just don't want it to update the cells linked to an external source.

The Open method takes an optional UpdateLinks parameter. Set this to false, and you should be fine.

Upvotes: 2

pabdulin
pabdulin

Reputation: 35235

There is a limitation in Excel engine which requires an opened workbook prior to setting XlCalculation property. So easiest solution would be to create a new workbook, and then set XlCalculation to desired mode, prior to opening file with actual data.

Upvotes: 2

Related Questions