Link to home
Start Free TrialLog in
Avatar of jper_hm
jper_hmFlag for Singapore

asked on

Excel Automation:Manual Calculation problem

Hi Experts,

I want to do Manual Calculation in Excel Automation with VC++/MFC. To do the equivalence of Excel/VBA

Application.Calculation = xlCalculationManual

I thought I could use

excel::_Application oExcel;
oExcel.SetCalculation((long)-4135);

Compilation goes fine, but error in run time. What's wrong here?

Thanks.
Avatar of mahesh1402
mahesh1402
Flag of India image

try this

vInt(calcn, -4135);
oExcel.SetCalculation(calcn);

SetCalculation() sets "calculation mode" of excel's active workbook.ie automatic or manual

check return value of function....
values are as follows
1 is for success
-1 / -2 excel/workbook not active
-3 unknown error

what value you r geting ? also what runtime error message you are getting ?

MAHESH
Avatar of jper_hm

ASKER

Thanks. What is vInt()? Which header file should I include?

JPER
what error u r getting .. pass it as a int

MAHESH
try

oExcel.SetCalculation(COleVariant((long)-4135));

MAHESH
Avatar of jper_hm

ASKER

I cannot compile with vInt() nor oExcel.SetCalculation(COleVariant((long)-4135));

vInt is not declared. I thought I should include some header file.

oExcel.SetCalculation(COleVariant((long)-4135)); is just not right. SetCalculation parameter is long and not VARIANT.
vInt was by mistake.....

but what error code you are getting ? -1 / -2 / -3 ?? an also whats ur runtime error ?

MAHESH
also post some more code
Avatar of jper_hm

ASKER

try
oExcel.SetCalculation(COleVariant((long)-4135));
get this compilation error:
'SetCalculation' : cannot convert parameter 1 from 'class COleVariant' to 'long'

Since I cannot run it, I do have the return value of the function.

There is nothing unique about my original code:
excel::_Application oExcel;
excel::Workbooks books;
excel::_Workbook book;
excel::Worksheets sheets;
excel::_Worksheet sheet;
excel::Range range, range1, cols;
oExcel.SetDisplayAlerts(0);
oExcel.SetVisible(TRUE);      
oExcel.SetUserControl(TRUE);
oExcel.SetScreenUpdating(FALSE); //to speed up update macro code
oExcel.SetCalculation((long)-4135); //xlCalculationManual=-4135

When it comes to the last line, an error ocurrs, saying "Runtime error! ...abnormal program termination"
I am asking about your original error....  when u compile your code with just

excel::_Application oExcel;
oExcel.SetCalculation((long)-4135);


what is the code returned ? and also whats the run time error ?

MAHESH
Avatar of jper_hm

ASKER

The error is "Runtime error! ...abnormal program termination".

If I step through the code, when it gets to oExcel.SetCalculation((long)-4135); I get "unhandled exception".

Since I get an error, I cannot get any returned code!
ASKER CERTIFIED SOLUTION
Avatar of mahesh1402
mahesh1402
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jper_hm

ASKER

I have already made the workbook visible and controllable.

oExcel.SetVisible(TRUE);    
oExcel.SetUserControl(TRUE);
i.e. 1st Activate Workbook and Worksheet and then Set calculation mode..

MAHESH
you need to open workbook and worksheet.. from your above code it seems you do not have any worksheet open in excel.. activate worksheet 1st and then set calculation mode

MAHESH
Avatar of jper_hm

ASKER

Mahesh, you are right. If SetCalculation() is done after opening a worksheet, it works fine.

Thank you.