HOWTO: Use MFC to Create a Microsoft Excel 97 Chart

Last reviewed: February 6, 1998
Article ID: Q178783
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++, 32-bit Editions, version 5.0
  • Microsoft Excel 97 for Windows

SUMMARY

This article discusses how to use version 4.2 of the Microsoft Foundation Class (MFC) library installed with Microsoft Visual C++ version 5.0 to automate Microsoft Excel 97 so that it will populate a worksheet with data and create charts.

MORE INFORMATION

You can copy the code in this article to the message handler function of an event defined in an MFC .cpp file. However, the purpose of the code is to illustrate the process of using the IDispatch interfaces and member functions defined in the Excel8.olb type library. The primary benefit comes from reading and understanding the code so that you can modify the example, or write code from scratch to automate Microsoft Excel 97.

Steps to Create the Project

  1. In Microsoft Excel, create a new workbook named Test.xls.

  2. Follow steps 1 through 12 in the following Microsoft Knowledge Base article to create a sample project that uses the IDispatch interfaces and member functions defined in the Excel8.olb type library:

          ARTICLE-ID: Q178749
    
          TITLE     : HOWTO: Create an Automation Project Using MFC and a Type
                      Library
    
    

  3. At the top of the AutoProjectDlg.cpp, add the following line:

          #include "excel8.h"
    

  4. Add the following code to CAutoProjectDlg::OnRun() in the AutoProjectDLG.cpp file.

    Sample Code -----------

          try
          {
           _Application app;  // app is the Excel 8 _Application object.
           _Workbook book;
           _Worksheet sheet;
           _Chart chart;
    

           Workbooks books;
           Worksheets sheets;
           Range range;
           ChartObjects chartobjects;
           Charts charts;
           LPDISPATCH lpDisp;
    

           // Common OLE variants. These are easy variants to use for
           // calling arguments.
           COleVariant
    
                      covTrue((short)TRUE),
                      covFalse((short)FALSE),
                      covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    
           // Start Excel and get the Application object.
           if(!app.CreateDispatch("Excel.Application"))
           {
            AfxMessageBox(
                 "Couldn't start Excel and get an application 0bject");
            return;
           }
    
           // Set visible.
           app.SetVisible(TRUE);
    
           // Get Workbooks collection.
           lpDisp = app.GetWorkbooks();  // Get an IDispatch pointer.
           ASSERT(lpDisp);
           books.AttachDispatch( lpDisp );  // Attach the IDispatch pointer
                                            // to the books object.
           // Open a workbook.
           lpDisp = books.Open("C:\\My Docs\\Test",
                       covOptional, covOptional, covOptional, covOptional,
                       covOptional, covOptional, covOptional, covOptional,
                       covOptional, covOptional, covOptional, covOptional
                              );
           ASSERT(lpDisp);  // It should have worked.
    
           // Attach to a Workbook object.
           book.AttachDispatch( lpDisp );  // Attach the IDispatch pointer
                                           // to the Workbook object.
    
           // Get sheets.
           lpDisp = book.GetSheets();
           ASSERT(lpDisp);
           sheets.AttachDispatch(lpDisp);
    
           lpDisp = sheets.GetItem( COleVariant((short)(1)) );
           ASSERT(lpDisp);
           // Attach the lpDisp pointer to a Worksheet object.
           sheet.AttachDispatch(lpDisp);
    
           lpDisp = sheet.GetRange(COleVariant("A1"), COleVariant("W40"));
                                   // The range is from A1 to W40.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);  // Attach the IDispatch pointer
                                          // to the range object.
           range.Clear();  // Could be ClearContents().
    
           ::Sleep(500); // So you can see it happen.
    
           lpDisp = sheet.GetRange(COleVariant("A3"), COleVariant("A3"));
                                                     // From A3 to A3.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);  // Attach the IDispatch pointer
                                          // the range object.
           range.SetValue(COleVariant("March")); // Insert March into range.
    
    
           // Following is a series of repetitive steps to populate the
           // worksheet's cells with a series of Months and values to be
           // used in the Chart object, which is yet to be constructed.
    
           lpDisp = sheet.GetRange(COleVariant("B3"), COleVariant("B3"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("12")); // Value for March.
    
           lpDisp = sheet.GetRange(COleVariant("A4"), COleVariant("A4"));
                // Months will be in column A, values in column B.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("April"));
    
           lpDisp = sheet.GetRange(COleVariant("B4"), COleVariant("B4"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("8"));
    
           lpDisp = sheet.GetRange(COleVariant("A5"), COleVariant("A5"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("May"));
    
           lpDisp = sheet.GetRange(COleVariant("B5"), COleVariant("B5"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("2"));
    
           lpDisp = sheet.GetRange(COleVariant("A6"), COleVariant("A6"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("June"));
    
           lpDisp = sheet.GetRange(COleVariant("B6"), COleVariant("B6"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("11"));
    
           lpDisp = sheet.GetRange(COleVariant("A7"), COleVariant("A7"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("July"));
    
           lpDisp = sheet.GetRange(COleVariant("B7"), COleVariant("B7"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("16"));
    
           // The cells are populated. To start the chart,
           // declare some long variables and site the chart.
           long left, top, width, height;
           left = 100;
           top = 10;
           width = 350;
           height = 250;
    
           lpDisp = sheet.ChartObjects(covOptional);
    
           ASSERT(lpDisp);
           chartobjects.AttachDispatch(lpDisp); // Attach the lpDisp pointer
                                       // for ChartObjects to the chartobjects
                                       // object.
           chartobjects.Add(left, top, width, height); // Defines the rectangle
    
           lpDisp = chartobjects.Item( COleVariant((short)(1)) ); //First chart
           ASSERT(lpDisp);
           ChartObject chartobject(lpDisp);  // Declared and constructed
                                             // in one step.
           chart.AttachDispatch(chartobject.GetChart()); // GetChart() returns
                                             // LPDISPATCH, & this attaches it
                                             // to your chart object.
    
           lpDisp = sheet.GetRange(COleVariant("A3"), COleVariant("B7"));
                             // The range containing the data to be charted.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
    
           VARIANT var; // ChartWizard needs a Variant for the Source range.
           var.vt = VT_DISPATCH; // .vt is the usable member of the tagVARIANT
                                 // Struct. Its value is a union of options.
           var.pdispVal = lpDisp; // Assign IDispatch pointer
                                  // of the Source range to var.
    
           chart.ChartWizard(var,                    // Source.
                            COleVariant((short)11),  // Gallery: 3d Column.
                            covOptional,             // Format, use default.
                            COleVariant((short)1),   // PlotBy: xlRows.
                            COleVariant((short)0),   // CategoryLabels.
                            COleVariant((short)1),   // SeriesLabels.
                            COleVariant((short)TRUE),  // HasLegend.
                            COleVariant("Use by Month"),  // Title.
                            COleVariant("Month"),    // CategoryTitle.
                            COleVariant("Usage in Thousands"),  // ValueTitles.
                            covOptional              // ExtraTitle.
                            );
           // The return is void.
           ::Sleep(3000);
           chartobject.Delete();  // Removes the first chartobject, sets the
           // ChartObjects.Item() count to 0. The next chart will restore the
           // item count to 1.
           ::Sleep(3000);  // Set the selected range to be erased.
           range.Clear();  // Erase the usage data.
    
           // Beginning of chart 2.
           lpDisp = sheet.GetRange(COleVariant("B3"), COleVariant("B3"));
                                          // From B3 to B3.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);  // Attach the IDispatch pointer
                                          // to the range object.
           range.SetValue(COleVariant("Chocolate")); // Insert Chocolate into
                                                     // the range object.
    
           // Following is a series of repetitive steps to populate the
           // worksheet's cells with a series of Flavors and values to be
           // used in the chart object, your second chart.
    
           lpDisp = sheet.GetRange(COleVariant("B4"), COleVariant("B4"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("12")); // Value for Chocolate.
    
           lpDisp = sheet.GetRange(COleVariant("C3"), COleVariant("C3"));
                // Flavors will be in row 3, values in row 4.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("Vanilla"));
    
           lpDisp = sheet.GetRange(COleVariant("C4"), COleVariant("C4"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("8"));
    
           lpDisp = sheet.GetRange(COleVariant("D3"), COleVariant("D3"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("Orange"));
    
           lpDisp = sheet.GetRange(COleVariant("D4"), COleVariant("D4"));
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
           range.SetValue(COleVariant("6"));
    
           // The cells are populated. To start the chart,
           // define the bounds, and site the chart.
    
           left = 250;
           top = 40;
           width = 300;
           height = 300;
    
           lpDisp = sheet.ChartObjects(covOptional);
    
           ASSERT(lpDisp);
           chartobjects.AttachDispatch(lpDisp); // Attach the lpDisp pointer
                                       // for ChartObjects to the chartobjects
                                       // object.
           chartobjects.Add(left, top, width, height); // Adds 1 to item count.
    
           //**************************************
           lpDisp = chartobjects.Item( COleVariant((short)(1)) );  // It was
               // zero, but just added one at a new location,
               // with new left, top, width, and height.
           ASSERT(lpDisp);
           chartobject.AttachDispatch(lpDisp); // Use definition of new chart
                                               // site.
           chart.AttachDispatch(chartobject.GetChart());
           //**************************************
    
           lpDisp = sheet.GetRange(COleVariant("B3"), COleVariant("D4"));
                             // Range containing the data to be charted.
           ASSERT(lpDisp);
           range.AttachDispatch(lpDisp);
    
           var.pdispVal = lpDisp; // Contains IDispatch pointer
                                  // to the Source range.
    
           chart.ChartWizard(var,                    // Source.
                            COleVariant((short)11),  // Gallery = 3D Column.
                            covOptional,             // Format, use default.
                            COleVariant((short)2),   // PlotBy xlColumns.
                            COleVariant((short)0),   // CategoryLabels.
                            COleVariant((short)1),   // SeriesLabels.
                            COleVariant((short)TRUE),  // HasLegend.
                            COleVariant("Use by Flavor"),  // Title.
                            COleVariant("Flavor"),    // CategoryTitle.
                            COleVariant("Usage in Barrells"),  // ValueTitles.
                            covOptional              // ExtraTitle.
                            );
           // The return is void.
           ::Sleep(3000);
    
           //Show the chart in Print Preview.
           chart.PrintOut(COleVariant((short)1),     // From (page #).
                          COleVariant((short)1),     // To (page #).
                          COleVariant((short)1),     // Copies.
                          COleVariant((short)TRUE),  // Preview.
                          covOptional,               // ActivePrinter.
                          covFalse,                  // PrintToFile.
                          covFalse                   // Collate.
                          );
    
           book.SetSaved(TRUE); // Avoids the 'Save changes?' dialog box.
           app.Quit(); // Excel departs.
    
           // By default, the pointer references for the objects
           // range, book, chart, chartobjects, sheet, and app
           // are automatically released when they go out of scope.
           // ReleaseDispatch()s are unnecessary.
    
           ::Sleep(1000);
           AfxMessageBox("Just executed App.Quit()");
    
          }  // End of processing logic.
    
          catch(COleException *e)
          {
            char buf[1024];
            sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc);
            ::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK);
          }
    
          catch(COleDispatchException *e)
          {
           char buf[1024];
           sprintf(buf,
                   "COleDispatchException. SCODE: %08lx, Description: \"%s\".",
                   (long)e->m_wCode,
                   (LPSTR)e->m_strDescription.GetBuffer(1024));
           ::MessageBox(NULL, buf, "COleDispatchException",
                        MB_SETFOREGROUND | MB_OK);
          }
    
          catch(...)
          {
           ::MessageBox(NULL, "General Exception caught.", "Catch-All",
                        MB_SETFOREGROUND | MB_OK);
          }
    
    

  5. You may need to modify the code in CAutoProjectDlg::OnRun() to indicate the correct path for your workbook Test.xls. The workbook is referenced in the following line:

          lpDisp = books.open("C:\\My Docs\\Test", . . .);
    


Additional query words: IDispatch graph xl8 Excel 8.0 Excel97 xl97

Keywords : MfcOLE kbcode kbinterop
Technology : kbOle
Version : Win95:5.0; WINNT:5.0
Platform : Win95 winnt
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 6, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.