home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / comexcel / excel8 / comexcel.cpp next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  3.1 KB  |  93 lines

  1. // Copyright (C) 1992-1998 Microsoft Corporation
  2. // All rights reserved.
  3. //
  4. // This source code is only intended as a supplement to the
  5. // Microsoft Visual C++ Language  Reference and related
  6. // electronic documentation provided with Microsoft Visual C++.
  7. // See these sources for detailed information regarding the
  8. // Microsoft Visual C++ product.
  9.  
  10. // NOTE: This example will only work with Excel8 in Office97
  11. // Compile with cl /GX comexcel.cpp
  12. // TO DO: Edit the #import paths
  13. #pragma message ("Make sure you go to Tools.Options.Directories and add the paths to mso97.dll and vbeext1.olb.  Mso97.dll will usually be in c:\\\"Program Files\"\\\"Microsoft Office\"\\Office, and vbeext1.olb will be in c:\\\"Program Files\"\\\"Common Files\"\\\"Microsoft Shared\"\\VBA")
  14. #import <mso97.dll> no_namespace rename("DocumentProperties", "DocumentPropertiesXL")   
  15. #import <vbeext1.olb> no_namespace   
  16. #import <excel8.olb> rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") no_dual_interfaces
  17.  
  18. #pragma warning (disable:4192)
  19.  
  20. #include <stdio.h>
  21. #include <tchar.h>
  22.  
  23. void dump_com_error(_com_error &e)
  24. {
  25.     _tprintf(_T("Oops - hit an error!\n"));
  26.     _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
  27.     _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
  28.     _bstr_t bstrSource(e.Source());
  29.     _bstr_t bstrDescription(e.Description());
  30.     _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
  31.     _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
  32. }
  33.  
  34. // If this is placed in the scope of the smart pointers, they must be
  35. // explicitly Release(d) before CoUninitialize() is called.  If any reference
  36. // count is non-zero, a protection fault will occur.
  37. struct StartOle {
  38.     StartOle() { CoInitialize(NULL); }
  39.     ~StartOle() { CoUninitialize(); }
  40. } _inst_StartOle;
  41.  
  42. void main()
  43. {
  44.     using namespace Excel;
  45.  
  46.     _ApplicationPtr pXL;
  47.  
  48.     try {
  49.     pXL.CreateInstance(L"Excel.Application.8");
  50.  
  51.     pXL->Visible = VARIANT_TRUE;
  52.  
  53.     WorkbooksPtr pBooks = pXL->Workbooks;
  54.     _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);
  55.  
  56.     _WorksheetPtr pSheet = pXL->ActiveSheet;
  57.         
  58.     try {
  59.         // This one will fail
  60.         pSheet->Name = "Market Share?";
  61.     } catch (_com_error &e) {
  62.         dump_com_error(e);
  63.     }
  64.  
  65.     pSheet->Name = "Market Share!";
  66.  
  67.     pSheet->Range["A2"]->Value = "Company A";
  68.     pSheet->Range["B2"]->Value = "Company B";
  69.     pSheet->Range["C2"]->Value = "Company C";
  70.     pSheet->Range["D2"]->Value = "Company D";
  71.  
  72.     pSheet->Range["A3"]->Value = 75.0;
  73.     pSheet->Range["B3"]->Value = 14.0;
  74.     pSheet->Range["C3"]->Value = 7.0;
  75.     pSheet->Range["D3"]->Value = 4.0;
  76.     
  77.     Sleep(1000);
  78.  
  79.     RangePtr  pRange  = pSheet->Range["A2:D3"];
  80.     _ChartPtr  pChart  = pBook->Charts->Add();
  81.     
  82.     pChart->ChartWizard((Range*) pRange, (long) xl3DPie, 7L, (long) xlRows,
  83.         1L, 0L, 2L, "Market Share");
  84.  
  85.     Sleep(6000);
  86.  
  87.     pBook->Saved = VARIANT_TRUE;
  88.     pXL->Quit();
  89.     } catch(_com_error &e) {
  90.     dump_com_error(e);
  91.     }
  92. }
  93.