We help IT Professionals succeed at work.

Question on : Excel Chart automation with VC++

Last Modified: 2007-12-19
I'm new to Office automation with VC++. I wanted to create a graph in Excel from my VC++ program. I looked around and found some code at some coding site and used it with my objects. I have created Excel objects from the type library automatically created classes:


CApplication oExcel; // this class is an auto created wrapper for _Application object
CWorkbook oBook;  // same for this and so on....

CWorkbooks oBooks;
CWorksheets oSheets;
CWorksheet oSheet;
CRange oRange;

// If you have not created Excel, create a new instance.
if (oExcel.m_lpDispatch == NULL) {

// Show Excel to the user.

// Add a new workbook and get the first worksheet in that book.
oBooks = oExcel.get_Workbooks();
oBook = oBooks.Add(covOptional);
oSheets = oBook.get_Worksheets();
oSheet = oSheets.get_Item(COleVariant((short)1));      

// Get a range of data.
oRange = oSheet.get_Range(COleVariant("A1"),covOptional);
oRange = oRange.get_Resize(COleVariant((short)5),COleVariant((short)5));

// here i'm putting some int values into the sheet just for testing the graph
COleSafeArray saRet;
DWORD numElements[2];
numElements[0] = 5;
numElements[1] = 5;
long index[2];

// Create a double safe array

// Fill the array with data.
for (int iRow = 1; iRow <= 5; iRow++) {
          for (int iCol = 1; iCol <= 5; iCol++) {
      double d = iRow + 2*iCol;
// Send the array to Excel.

// now create the graph      

CChartObjects ccobj(oSheet.ChartObjects(covOptional));
int left = 100;
int top = 100;
int width = 350;
int height = 250;

CChartObject chartobject = ccobj.Add(left, top, width, height);

CChart chart(chartobject.get_Chart());

LPDISPATCH lpdisp = oSheet.get_Range(COleVariant("A1"), COleVariant("B5"));
// Range containing the data to be charted.

CRange range(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.
COleVariant covTF = COleVariant((short)FALSE);

chart.ChartWizard(     var, // Source.
            COleVariant((short)3), // Gallery = xy. (?? where do we get this number)
            COleVariant((short)1), // Format, use default. (?? same here and so on)
            COleVariant((short)2), // PlotBy xlColumns.
            COleVariant((short)1), // CategoryLabels.
            COleVariant((short)0), // SeriesLabels.
            covTF, // HasLegend. // i understand this - means no legend
            COleVariant("Title"), // Title. (ok, this means set the title as "Title")
            COleVariant("Frequency"), // CategoryTitle.
            COleVariant("Whatever"), // ValueTitles.
            covOptional // ExtraTitle.

Ok. This creates the chart. The problem is, there is absolutely no documentation on the objects here for vc++ - i havent seen any. I went thru the mfc support links and found some sample code but no documentation.

I dont know how to change this chart or change the color or manipulate it. i can guess with the function names and tried chaning the integer values in the chatwizard and it produces weird results at times and errors at times. Does anyone have any documentation on these functions.
Is there any sample code i can use to see how to manipulate charts? And can i select range from different places in excel instead of the initial range (A5-B5 - here)?

Any help is greatly appreciated.
Thanks a lot,
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)


Thanks for your answer. Do you know we can run VB code in VC++. That should then solve my problem.
Do I get the points?


Sure. I didnt get the answer with vc++ put your answer gave me some other pointers to work on. Thanks a lot for that. I'll give you the points.
Thanks and happy coding.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.