Microsoft KB Archive/186120

= How To Use MFC to Automate Excel and Fill a Range with an Array =

Article ID: 186120

Article Last Modified on 1/23/2007

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 97 Standard Edition

-



This article was previously published under Q186120



SUMMARY
This article demonstrates how to automate Microsoft Excel and fill a multi-cell range with an array of values.



MORE INFORMATION
To fill a multi-cell range without populating the cells one-by-one, you must create a two-dimensional variant SAFEARRAY which you pass to Excel by calling the SetValue function for the Range object. The following steps illustrate this process.

Notes for Automating Microsoft Excel 2000 and 2002
The sample code in this article uses class wrappers generated from the Excel 97 object library (Excel 8.olb). With slight modification, this code can be applied to an Automation client that uses class wrappers for Excel 2000 (Excel9.olb) or Excel 2002 (Excel.olb). For additional information about using the sample code described in this article with the Microsoft Excel 2000 or 2002 type library, please click the article number below to view it in the Microsoft Knowledge Base:

224925 INFO: Type Libraries for Office May Change With New Release

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

178749 How To Create an Automation Project Using MFC and a Type Library

  To the dialog box created in steps 4 and 5 of the parent article 178749, add the following controls with properties as specified. Also add the corresponding member variables:                                  Member                Member Control  Name              Variable Type         Variable Name -     Edit      IDC_STARTINGCELL  m_sStartingCell       CString Edit     IDC_NUMROWS       m_iNumRows            short Edit     IDC_NUMCOLS       m_iNumCols            short CheckBox IDC_STRING        m_bFillWithStrings    BOOL   At the top of the AutoProjectDlg.cpp file, add the following line: #include "excel8.h"                     Add the following code to CAutoProjectDlg::OnRun in the AutoProjectDlg.cpp file.

Sample Code
// OLE Variant for Optional. COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

_Application objApp;

_Workbook objBook; Workbooks objBooks; Worksheets objSheets; _Worksheet objSheet; Range range;

if(!UpdateData(TRUE)) {        return; }

// Instantiate Excel and start a new workbook. objApp.CreateDispatch("Excel.Application"); objBooks = objApp.GetWorkbooks; objBook = objBooks.Add(VOptional); objSheets = objBook.GetWorksheets; objSheet = objSheets.GetItem(COleVariant((short)1));

//Get the range where the starting cell has the address //m_sStartingCell and it's dimensions are m_iNumRows x m_iNumCols. range = objSheet.GetRange(COleVariant(m_sStartingCell),                               COleVariant(m_sStartingCell)); range = range.GetResize(COleVariant(m_iNumRows),                             COleVariant(m_iNumCols));

//*** Fill the range with an array of values.

//Create the SAFEARRAY. COleSafeArray saRet; DWORD numElements[2]; numElements[0]= m_iNumRows;  //Number of rows in the range. numElements[1]= m_iNumCols;  //Number of columns in the range.

if(m_bFillWithStrings) {        saRet.Create(VT_BSTR, 2, numElements); }     else {        saRet.Create(VT_R8, 2, numElements); }

//Fill the SAFEARRAY. long index[2]; long iRow; long iCol;

for(iRow=0;iRow<=m_iNumRows-1;iRow++) {        for(iCol=0;iCol<=m_iNumCols-1;iCol++) {           index[0] = iRow; index[1] = iCol; if(m_bFillWithStrings)     //Fill with Strings. {              VARIANT v;               CString s;               VariantInit(&v); v.vt = VT_BSTR; s.Format("r%dc%d", iRow, iCol); v.bstrVal = s.AllocSysString; saRet.PutElement(index, v.bstrVal); SysFreeString(v.bstrVal); VariantClear(&v); }           else                     //Fill with Numbers. {              double d;               d = (iRow*1000) + iCol; saRet.PutElement(index, &d); }        }      }

//Set the range value to the SAFEARRAY. range.SetValue(COleVariant(saRet)); saRet.Detach;

//Return control of Excel to the user. objApp.SetVisible(TRUE); objApp.SetUserControl(TRUE);  Compile and Run the project.  Specify the following values for the controls on the dialog box: <pre class="fixed_text">     Control               Contents --     IDC_STARTINGCELL      A1      IDC_NUMROWS           10 IDC_NUMCOLS          5 IDC_STRING           True Click OK.

Results: A new workbook is generated and cells A1:E10 of the first worksheet are populated with string values. </li>  Specify the following values for the controls on the dialog box: <pre class="fixed_text">     Control               Contents --     IDC_STARTINGCELL      C3      IDC_NUMROWS           2 IDC_NUMCOLS          9 IDC_STRING           False Click OK.

Results: A new workbook is generated and cells C3:K4 of the first worksheet are populated with numeric values. </li></ol>

<div class="references_section">