Sample C++ Code: Formula Calculations

#include "stdafx.h"

 

#import <Pagos.Spreadsheet.COM.dll>

 

using namespace PagosSpreadSheet;

using namespace std;

 

//    Creates PSC Application interface

void CreatePSCInterface(IApplication **app)

{

      //    Initialize COM subsystem

      CoInitialize(NULL);

 

      //    Accessing com object

      HRESULT hr = CoCreateInstance(__uuidof(Application), NULL, CLSCTX_ALL, __uuidof(IApplication), static_cast<void**>(static_cast<void*>(app)));

 

      //    Checking for error

      if(!SUCCEEDED(hr))

      {

            LPVOID lpMsgBuf;

            FormatMessage(

                  FORMAT_MESSAGE_ALLOCATE_BUFFER |

                  FORMAT_MESSAGE_FROM_SYSTEM |

                  FORMAT_MESSAGE_IGNORE_INSERTS,

                  NULL,

                  hr,

                  0, // Default language

                  (LPTSTR) &lpMsgBuf,

                  0,

                  NULL

                  );         

            wcout << ((wchar_t *)lpMsgBuf);

            LocalFree( lpMsgBuf );

            cout << "Error while creating com object." << endl;

            system("PAUSE");

            exit(-1);

      }

}

 

int main(int argc, char* argv[])

{

     

      try

      {

            long start = GetTickCount();

 

            IApplication *app = NULL;

 

            //Create an instance of the application

            CreatePSCInterface(&app);

 

            //Add a workbook

            IWorkbooks *wbs;

            HRESULT hr = app->get_Workbooks(&wbs);

            if (FAILED(hr))

                  return EXIT_FAILURE;

 

            //Open quote template workbook

            wbs->Open("..\\Spreadsheets\\Retirement\\Retirement.xls");

 

            IWorkbook *wb;

            hr = wbs->get_Item(_variant_t("Retirement.xls"),&wb);

            if (FAILED(hr))

                  return EXIT_FAILURE;

 

            //Set input values          

            float val;

 

            std::cout << "Current_Age: ";

            std::cin >> val;

            wb->GetNames()->Item["Current_Age"]->Range->Value = val;

 

            std::cout << "Desired_Retirement_Age: ";

            std::cin >> val;

            wb->GetNames()->Item["Desired_Retirement_Age"]->Range->Value = val;

 

            std::cout << "Monthly_savings: ";

            std::cin >> val;

            wb->GetNames()->Item["Monthly_savings"]->Range->Value = val;

 

            std::cout << "Annual_Investment_growth_rate: ";

            std::cin >> val;

            wb->GetNames()->Item["Annual_Investment_growth_rate"]->Range->Value = val;

 

            std::cout << "Annual_Salary_Increase_Rate: ";

            std::cin >> val;

            wb->GetNames()->Item["Annual_Salary_Increase_Rate"]->Range->Value = val;

 

            wcout << L"Retirement_Nest_Egg:" << wb->GetNames()->Item["Retirement_Nest_Egg"]->Range->Value.dblVal << endl;

 

            //Close workbook

            wb->Close();

      }

      catch(_com_error &e)

      {

            cout << e.Description() << endl;

      }

 

      CoUninitialize();

      system("PAUSE");

      return 0;

}