Problem with COleSafeArray or Range::SetValue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Problem with COleSafeArray or Range::SetValue

  1. #1
    Join Date
    Feb 2012
    Posts
    3

    Unhappy Problem with COleSafeArray or Range::SetValue

    I'm trying to fill an Excel range with an array.

    my code is based on this article:
    http://support.microsoft.com/kb/186120

    My problem is, instead of filling the cells with d = 0.006 + k

    every cell ends up as 0.006

    I tried adding the: if (true) {...} in case I needed d to go out of scope, but it didn't work. Any ideas what the problem might be?

    Other differences from the help article is I'm working with excel 2007 hence the "range.setValue2(...)". Also Visual C++ 6


    Code:
    range = sheet.GetRange(cell, cell);
    range = range.GetResize(COleVariant(365L),COleVariant(1L));
    	
    // fill safe array value by value....
    
    COleSafeArray saRet;
    DWORD numElements[1];
    numElements[0] = 365;
    saRet.Create(VT_R8, 1, numElements);
    
    long index[1];
    long k;
    
    //Fill the Safe Array with the column's data
    for (k = 0; k < 365; k++) 
    {
    	index[0] = k;
    	if (true)
    	{
    		double d;
    		d = 0.006+k; 
    		saRet.PutElement(index, &d);
    	}
    }
    
    range.SetValue2(COleVariant(saRet));
    saRet.Detach();
    				 
    // Make Excel visible
    app.SetVisible(TRUE);
    
    // Return control of Excel to user
    app.SetUserControl(TRUE);

  2. #2
    Join Date
    Feb 2012
    Posts
    3

    Re: Problem with COleSafeArray or Range::SetValue

    I added this:

    Code:
    double val;
    for (index[0] = 0; index[0]  < 365; index[0] ++) 
    {
    		saRet.GetElement(index, &val);
                    ASSERT("&#37;1.4f\n", val);
    }
    and it outputs what is expected {0.006, 1.006, 2.006...}

    So the problem must be with

    Code:
    range.SetValue2(COleVariant(saRet));
    ...help? =)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,229

    Re: Problem with COleSafeArray or Range::SetValue

    How do you know the values are all the same?

    FWIW, you can use braces to delineate scope without the if statement.

    Code:
    	{
    		double d;
    		d = 0.006+k; 
    		saRet.PutElement(index, &d);
    	}
    That would have done the same thing.

  4. #4
    Join Date
    Feb 2012
    Posts
    3

    Re: Problem with COleSafeArray or Range::SetValue

    Thanks for the reply.

    The values are all the same when they reach excel, i.e. I have a column full of 0.006's instead of 0.006, 1.006, 2.006... etc). From the ASSERT I added, I can see the values are correct within the SafeArray, but they all take on a single value (apparently the first value, i.e. safearray element 0) when I try to put give the array to excel with

    range.SetValue2(COleVariant(saRet));

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center