Solved

Excel   _Application.Run with array as parameter

Posted on 2000-05-15
1
289 Views
Last Modified: 2013-11-20
In vb the call looks like this

ExcelApp---Excel.Application

dim MyArray(2)as double
MyArray(1)=1
MyArray(2)=2
ExcelApp.Run("SomeFunc",MyArray)
Works well

How can I do that in MFC
My code is like:
_______________________________________
COleVariant FuncName;FuncName.bstrVal= SysAllocString(L"SomeFunc");
COleVariant MyArray;
double tmp[2]={1,2};
PutValues(&MyArray,tmp,2);
m_ExcelApp.Run(FuncName,MyArray);


The put values function is:
_______________________________________
void PutValues(COleVariant  *ThisArray, double *Arr, long nCount)
{
      int i = 0;      
      long *palDims = 0;
      double      rValue = 0.0;
      SAFEARRAY *psaData = 0;
      SAFEARRAYBOUND *pabound = 0;
      HRESULT      hRes = 0;      

      VariantInit( ThisArray );
        pabound = (SAFEARRAYBOUND *) malloc( 2 * sizeof(SAFEARRAYBOUND) );
      palDims = (long *) malloc( 2 * sizeof(long) );
      if( pabound && palDims )
            {
            pabound[0].cElements = nCount;
            pabound[0].lLbound = 0L;
            pabound[1].cElements = 1;
            pabound[1].lLbound = 0L;
            psaData = SafeArrayCreate( VT_R8, 2, pabound );
            for( i = 0; ( i < nCount) && ( hRes == S_OK) ; i++ )
            {
                palDims[0] = i;
              palDims[1] = 0;
              hRes = SafeArrayPutElement( psaData, palDims,&Arr[i]);
            }
             ThisArray->parray = psaData;
                ThisArray ->vt = VT_R8;
            ThisArray->vt|= VT_ARRAY;
            }
            free( pabound );
            free( palDims );
}


0
Comment
Question by:mdanny
1 Comment
 
LVL 23

Accepted Solution

by:
naveenkohli earned 50 total points
ID: 2815250
Look at this Microsoft knowledge base article.

Article ID: Q186122
 

HOWTO: Use MFC to Automate Excel & Obtain an Array from a Range

http://support.microsoft.com/support/kb/articles/q186/1/22.asp

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question