Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open excel workbook but set calculations to manual

Tags:

c#

.net

I have tried a few options but none seem to work, and some send errors.

Please let me know what I am doing wrong

public string Main(String wbPath, String wbName)
{
    string cName = "";
    Excel.Application xlApp;
    Excel.Workbook xlWB;
    Excel.Worksheet xlWS;


    xlApp = new Excel.Application();
    xlApp.DisplayAlerts = false;
    xlApp.Calculation = Excel.XlCalculation.xlCalculationManual;       //Error occurs here 

    xlWB = xlApp.Workbooks.Open(wbPath + wbName);

    xlWB.SaveAs("vFile.html", Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml);
    cName = xlWB.FullName;
    xlWB.Close();


    xlApp.Quit();

    return cName;
}

Error code:

{"Exception from HRESULT: 0x800A03EC"}
like image 814
jason m Avatar asked Oct 07 '11 21:10

jason m


People also ask

How do I open Excel in manual Calculation mode?

To change the mode of calculation in Excel, follow these steps: Click the Microsoft Office Button, and then click Excel Options. On the Formulas tab, select the calculation mode that you want to use.

How do I change Formulas from automatic to manual in Excel?

On the Formulas tab, in the Calculation group, click Calculation Options, and then click Automatic.

How do you open Excel without running Formulas?

One way: open it in Manual calculation mode. To do that, double-click the Excel program icon (not the file icon) to start Excel with a new worksheet. Click Formula, Calculation Options, Manual.


1 Answers

You must open the workbook before setting the xlApp.Calculation:

static void Main(string[] args)
{
    string cName = "";

    var xlApp = new Application();

    var xlWB = xlApp.Workbooks.Open("youpathgoeshere", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    xlApp.Calculation = XlCalculation.xlCalculationManual;

    var xlWS = new Worksheet();

    xlWB.SaveAs("vFile.html", Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml);
    cName = xlWB.FullName;
    xlWB.Close();

    xlApp.Quit();

}
like image 162
KreepN Avatar answered Sep 23 '22 09:09

KreepN