LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to make an array with ActiveX Excel using Excel_RangeSetValue2?

Solved!
Go to solution

Hello,

 

I want to make an array on Excel. For that, I'm using two functions : Excel_WorksheetGetRange and Excel_RangeSetValue2.

I write the array on Excel file but only the first case of the array is copied.

 

Here, a part of my program :

 

char * DataToWrite = {"A", "B", "C", "D"}

char cCell[10];
VARIANT             vCell, Data;

ExcelObj_Worksheet hWorksheet;

ExcelObj_Range      hRange;

 

  ...

  CA_VariantSetCString (&vCell,cCell);        //cCell = A1:A4
  Excel_WorksheetGetRange (hWorksheet, NULL, vCell, CA_DEFAULT_VAL, &hRange);
  CA_VariantSetCString (&Data,DataToWrite );
  Excel_RangeSetValue2 (hRange, NULL, Data);

 

 

Here what I obtain when I'm compiling on Excel file :

A

A

A

A
 

0 Kudos
Message 1 of 3
(4,443 Views)
Solution
Accepted by Toto555555

You should use other instructions to fill the range of cells:

 

strcpy (msg, "A1:A4");
CA_VariantSetCString (&MyVariant, msg);
Excel_WorksheetRange (ExcelWorksheetHandle, NULL, MyVariant, CA_DEFAULT_VAL, &ExcelRangeHandle);
Excel_RangeActivate (ExcelRangeHandle, NULL, NULL);

// Create a SafeArray from array of data CA_VariantSet2DArray (&MyVariant, CAVT_CSTRING, righe, 1, array); // Transfer data to Excel range Excel_SetProperty (ExcelRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, MyVariant);
ClearObjHandle (ExcelRangeHandle);

 

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 3
(4,425 Views)

Thank you for your solution, it works. It helps me to find one with another functions : 

 
  char            strCell[10];
  VARIANT             vCell,vData;
  ExcelObj_Range      hRange;
 
  CA_VariantSetCString (&vCell, strCell);
  Excel_WorksheetGetRange (hWorksheet, NULL, vCell, CA_DEFAULT_VAL, &hRange);
  Excel_RangeActivate (hRange, NULL, NULL);

 

  // Create a SafeArray from array of data
  CA_VariantSet2DArray (&vData, CAVT_CSTRING, iNbElts, 1, strDataToWrite);


  // Transfer data to Excel range
  Excel_RangeSetValue2 (hRange, NULL, vData);

 

  CA_DiscardObjHandle (hRange);

0 Kudos
Message 3 of 3
(4,419 Views)