LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing Text to an Excel Spreadsheet Cell

I am using the Instruments ExcelReport and the MicroSoft Excel 9.0 Library and I am trying to write a string to a single cell in a spreadsheet.  I am able to open Excel and get the appropriate handles, I can write numeric data but not text.  Here is the code segment...
 
int Write1StringExcel (char *CellRange, char *WriteStr)
//This will write a single double to an excel spreadsheet
{
 int failure=-99;
 char errorBuf[256];
 BSTR arg1__AutoType=0;
 
 failure=CA_CStringToBSTR (WriteStr, &arg1__AutoType);
 CA_GetAutomationErrorString (failure, errorBuf, sizeof (errorBuf));
 // Write the value of the Single Cell Range
 failure = ExcelRpt_WriteData (ExcelWorksheetHandle, CellRange, ExRConst_dataString, 1, 1, arg1__AutoType);
 CA_GetAutomationErrorString (failure, errorBuf, sizeof (errorBuf)); 
 return (failure);
}
 
I discovered that just sending a char * in ExcelRpt_WriteData caused a general protection fault, so I tried creating a BSTR.  The function call works now but writes garbage.  I believe my problem is in the CA_CStringToBSTR call.  When I view the variable arg1 there is "No Size Information".  I cannot seem to find a method to set the size information of the BSTR.  Does anybody have any advice on how to either make the charachter pointer work or the Basic String work for the call to ExcelRpt_WriteData?
 
Thanks,
-don
Go Cubbies!!
0 Kudos
Message 1 of 3
(5,268 Views)

Don,

The last parameter to ExcelRpt_WriteData function is an array of size "Dimension 1" by "Dimension 2", where those values are the fourth and fifth parameters to the function.  In your case you want to pass string values, which are of type char*.  So in order to have an array of one string value, you must pass a value of type char**.  You can do this by passing the address of your char* value. 

Like this:

failure = ExcelRpt_WriteData (ExcelWorksheetHandle, CellRange, ExRConst_dataString, 1, 1, &WriteStr);

I assume you are using ExcelRpt_WriteData because you need to write a string value to a range of multiple cells.  If instead you are only writing this string value to a single cell, you can use the ExcelRpt_SetCellValue function to do this.  This function expects a single value instead of an array of values and would allow you to simply pass your char* value.
 
Hope this helps.
 
-Jeff
 
0 Kudos
Message 2 of 3
(5,255 Views)

Thanks Jeff-

That's the ticket.  It shouldn't have to be this hard.

-don

Go Cubbies!!
0 Kudos
Message 3 of 3
(5,246 Views)