Solved

Excel   _Application.Run with array as parameter

Posted on 2000-05-15
1
284 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 47
Complete beginner needs help making a cron job 9 103
matchUp  challenge 6 48
ShiftLeft challenge 21 65
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now