Filling an Excel range with an array using

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

The following 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(...)"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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);
Last edited on
would declaring the d outside the if statement make a difference?

as in :

1
2
3
4
5
double d;
if (true){
d = 0.006+k;
saRet.PutElement(index, &d);
}
Nope (I don't understand the suggestion, but I tried it anyway). According to the example in the link I shouldn't need the if(...) block in the first place. ARGH!!
I added this:

1
2
3
4
5
6
double val;
for (index[0] = 0; index[0]  < 365; index[0] ++) 
{
		saGet.GetElement(index, &val);
                ASSERT("%1.4f\n", val);
}


and it outputs what is expected {0.006, 1.006, 2.006...}

So the problem must be with

range.SetValue2(COleVariant(saRet));

...help? =)
Topic archived. No new replies allowed.