C++ and Excel 
Author Message
 C++ and Excel

A followup to my previous question. After doing some research, the #import
method of creating Excel spreadsheets seems to have problems. I've decided
to use automation and it seems to be a bit more flexible. Here is my problem
though.

Is there a way to set particular cells? For example, I know you can specify
a Range, such as A1 - B6, but I'm wanting to use it in a loop. I have not
found a way to do something like the following:

for (i=0; i<=5; i++)
{
    Range("A" + i, "B"  + i);

Quote:
}

of course this is not entirely correct, the you get the point. The first
time through the loop I want A1-B1, the second time I want A2-B2, etc.
Unfortunately I can't find a way to use Range within a loop like this, nor
is there a method of setting Cells.Value. Any ideas?

Cedric



Sat, 10 Sep 2005 21:30:02 GMT  
 C++ and Excel
I'm not sure what your Range() function does but try this, or a variant of
it:

CString str;
for (int i = 0; i <= 5; i++)
{
    str.Format("A%d B%d", i, i);
    Range(str);

Quote:
}



Quote:
> A followup to my previous question. After doing some research, the #import
> method of creating Excel spreadsheets seems to have problems. I've decided
> to use automation and it seems to be a bit more flexible. Here is my
problem
> though.

> Is there a way to set particular cells? For example, I know you can
specify
> a Range, such as A1 - B6, but I'm wanting to use it in a loop. I have not
> found a way to do something like the following:

> for (i=0; i<=5; i++)
> {
>     Range("A" + i, "B"  + i);
> }

> of course this is not entirely correct, the you get the point. The first
> time through the loop I want A1-B1, the second time I want A2-B2, etc.
> Unfortunately I can't find a way to use Range within a loop like this, nor
> is there a method of setting Cells.Value. Any ideas?

> Cedric



Sat, 10 Sep 2005 22:00:40 GMT  
 C++ and Excel
I was hoping for something more alongs the line of an array. Such as:

sheet.Cells[2][4]

would be cell 2,4. I know this is possible when you use #import to create an
Excel spreadsheet, but not with automation. Any ideas?

Cedric



Quote:
> I'm not sure what your Range() function does but try this, or a variant of
> it:

> CString str;
> for (int i = 0; i <= 5; i++)
> {
>     str.Format("A%d B%d", i, i);
>     Range(str);
> }



> > A followup to my previous question. After doing some research, the
#import
> > method of creating Excel spreadsheets seems to have problems. I've
decided
> > to use automation and it seems to be a bit more flexible. Here is my
> problem
> > though.

> > Is there a way to set particular cells? For example, I know you can
> specify
> > a Range, such as A1 - B6, but I'm wanting to use it in a loop. I have
not
> > found a way to do something like the following:

> > for (i=0; i<=5; i++)
> > {
> >     Range("A" + i, "B"  + i);
> > }

> > of course this is not entirely correct, the you get the point. The first
> > time through the loop I want A1-B1, the second time I want A2-B2, etc.
> > Unfortunately I can't find a way to use Range within a loop like this,
nor
> > is there a method of setting Cells.Value. Any ideas?

> > Cedric



Sat, 10 Sep 2005 22:13:28 GMT  
 C++ and Excel
Here is the actual code:

COleVariant str1, str2;
 for (int i = 0; i <= 5; i++)
 {
  str1 = "A" + i;
  str2 = "B" + i;
  lpDisp = sheet.GetRange(COleVariant(str1), COleVariant(str2));
  ASSERT(lpDisp);
  range.AttachDispatch(lpDisp);
  range.SetValue(COleVariant("200"));
 }

if I set str1 to "A1" and str2 to "B1" it all works fine. It does not work
with the variable in it. I've tried typecasting it, but when I run the
program, I get "COleDispatchException. SCODE: 00000000, Description: "". "
Any ideas?

Cedric


Quote:
> I'm not sure what your Range() function does but try this, or a variant of
> it:

> CString str;
> for (int i = 0; i <= 5; i++)
> {
>     str.Format("A%d B%d", i, i);
>     Range(str);
> }



> > A followup to my previous question. After doing some research, the
#import
> > method of creating Excel spreadsheets seems to have problems. I've
decided
> > to use automation and it seems to be a bit more flexible. Here is my
> problem
> > though.

> > Is there a way to set particular cells? For example, I know you can
> specify
> > a Range, such as A1 - B6, but I'm wanting to use it in a loop. I have
not
> > found a way to do something like the following:

> > for (i=0; i<=5; i++)
> > {
> >     Range("A" + i, "B"  + i);
> > }

> > of course this is not entirely correct, the you get the point. The first
> > time through the loop I want A1-B1, the second time I want A2-B2, etc.
> > Unfortunately I can't find a way to use Range within a loop like this,
nor
> > is there a method of setting Cells.Value. Any ideas?

> > Cedric



Sat, 10 Sep 2005 22:44:56 GMT  
 C++ and Excel
Quote:
>   str1 = "A" + i;
>   str2 = "B" + i;
and
> if I set str1 to "A1" and str2 to "B1" it all works fine. It does not work

your setting for "A1" and "B1" work but not "A" + i cause "A" + i add the
value if i to the address of the C String literal "A" and not adding a the
digit to form at two BYTE string like you want... Aside of the excel stuff
you want like David Crow suggested str.Format("A%d B%d", i, i); or
str1.Format("A%d", i); and str2.Format("B%d", i); in your newest post
instead to the two line str1 = "A" + i; and str2 = "B" + i; and when these
two line are done you will have "A1" and "B2" just as you tried with you
hardcoded test.


Sun, 11 Sep 2005 04:26:26 GMT  
 C++ and Excel
That first part didn't come out right so the update is below...

Quote:
> if I set str1 to "A1" and str2 to "B1" it all works fine.

It work in your setting of "A1" and "B1" but not "A" + i cause "A" + i add
the value if i to the address of the C String literal "A" and not adding a
digit to form at two BYTE string like you want... Aside of the excel stuff
you want like David Crow suggested str.Format("A%d B%d", i, i); or
str1.Format("A%d", i); and str2.Format("B%d", i); in your newest post
instead to the two line str1 = "A" + i; and str2 = "B" + i; and when these
two line are done you will have "A1" and "B2" just as you tried with you
hardcoded test.


Sun, 11 Sep 2005 04:32:28 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. IRTDServer implementation in C++ for Excel XP (2002)

2. C++ and Excel

3. Visual C++ and Excel

4. C++ and Excel

5. C++ and Excel

6. C++ to Excel..

7. Visual C++ and Excel

8. VISUAL C++ AND EXCEL

9. EXPORTING to EXCEL with Visual C++ Net ???

10. Excel suport for C++ .NET?

11. Reading Excel file using C/C++

12. reading/writing into excel file from c/c++ application

 

 
Powered by phpBB® Forum Software