excel_setproperty

Hello

I would like to create an Excel report, with the text and data in different cells.  For example:

Voltage: 12.345

Power supply: 6653A Agilent

The excel2000dem.cws shows how to write data to excel.  I have tried the excel_setproperty command.  It is perfectly OK to write data, but not quite successful to write text.  I already tried different tyes of VARIANTS.

Please notify

In case you need to write a single cell, you can use

CA_VariantSetCString (& MyVariant, 'Text');
Excel_RangeSetItem (ExcelRangeHandle, CA_VariantInt (row), CA_VariantInt (column), MyVariant, NULL);

On the other hand, to write a string in a column array, if that's what you want to do, you can use this function:

//----------------------------------------------------------------------
//
Function ArrayTransfer)
//
//----------------------------------------------------------------------
HIFN ArrayTransfer)
HIFN transfer a table to a column in an Excel sheet
HIRET error code (0 if all goes well)
HIPAR column / column to write to
HIPAR nRows/number of elements in the array
Variant of HIPAR MyVariant/work
HIPAR-ExcelWorksheetHandle/handle of the sheet to write to
HIPAR-ExcelRangeHandle/handle of the range of cells that you want to write
HIPAR aType/table data type
HIPAR/table to trasfer data
int ArrayTransfer (int column, int nRows, VARIANT MyVariant, CAObjHandle ExcelWorksheetHandle, CAObjHandle * ExcelRangeHandle, unsigned int aType, void * array)

{
int error = 0;
char msg [64];

If (column > 52) / / in the case of 53 + columns
sprintf (msg, ' B % c2: B %c %d ", 64 + column - 52, 64 + column - 52, nRows);
ElseIf (column > 26) / / in the case of 26 + columns
sprintf (msg, ' a c2%: a %d %c ", 64 + column - 26, 64 + column - 26, nRows);
on the other
sprintf (msg, "% c2: %c %d", 64 + column, 64 + column, nRows);
errChk (CA_VariantSetCString (& MyVariant, msg));
If (* ExcelRangeHandle) errChk (ClearObjHandle (ExcelRangeHandle));
errChk (Excel_WorksheetRange (ExcelWorksheetHandle, NULL, MyVariant, CA_DEFAULT_VAL, ExcelRangeHandle));

switch (aType) {}
case CAVT_CSTRING:
errChk (CA_VariantSet2DArray (& MyVariant CAVT_CSTRING nRows, 1, (char *) table));
break;
case CAVT_DOUBLE:
errChk (CA_VariantSet2DArray (& MyVariant CAVT_DOUBLE nRows, 1, (double *) table));
break;
case CAVT_INT:
errChk (CA_VariantSet2DArray (& MyVariant CAVT_INT nRows, 1, (int *) table));
break;
}
errChk (Excel_SetProperty (* MyVariant, Excel_RangeValue2, ExcelRangeHandle, CAVT_VARIANT, NULL));
errChk (ClearObjHandle (ExcelRangeHandle));

Error:
error return;
}

The function can be called in this way:

int		i, error;
int nRows; Number of items
char * dh = NULL;
VARIANT MyVariant;

Allocate memory for strings
DH = malloc (nRows * sizeof (char *)); Enter data/ora
< nrows;="" i++)="">
DH [i] = malloc (50 * sizeof (char));
}

Fill the string array
// ....

Write on the Excel sheet
error = ArrayTransfer (column, nRows, MyVariant, ExcelWorksheetHandle, & ExcelRangeHandle, CAVT_CSTRING, dh);
Test error code

Code follows here
// ...

Tags: NI Software

Similar Questions

  • How to set up excel column width

    I use the ExcelRpt_SetCellRangeAttribute function to set the width of a cell in excel.

    What vale I should switch to set the width for example 50, I tried to transmit values, but his does not work.

    After passing the values'm specifying the column containing the cell is hidden.

    pls help me

    Hello!

    I modified the sample project "excel2000dem.prj" come with the installation of LabWindows/CVI to see if I can adjust the width of the column that you have requested.

    Following the partial code successfully set the column width to 15.0 points in MS Excel 2003.

    //----------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #define EXCEL_ARRAY_OF_CELLS "A2:H11".

    ...

    public static ExcelObj_App ExcelAppHandle = 0;
    public static ExcelObj_Workbooks ExcelWorkbooksHandle = 0;
    public static ExcelObj_Workbook ExcelWorkbookHandle = 0;
    public static ExcelObj_Window ExcelWindowHandle = 0;
    public static ExcelObj_Sheets ExcelSheetsHandle = 0;
    public static ExcelObj_Worksheet ExcelWorksheetHandle = 0;
    public static ExcelObj_Range ExcelRangeHandle = 0;

    public static MyCellRangeV VARIANT;

    ....... (I assume that you have an open Excel application and an active workbook already)...

    {

    Get the sheets in the active workbook
    error = Excel_GetProperty (ExcelAppHandle, NULL, Excel_AppSheets, CAVT_OBJHANDLE, & ExcelSheetsHandle);
                   
    Get the first worksheet
    error = Excel_SheetsItem (ExcelSheetsHandle, NULL, CA_VariantInt (1), & ExcelWorksheetHandle);
           
    The first sheet Active - should already be active
    error = Excel_WorksheetActivate (ExcelWorksheetHandle, NULL);

    New range open to the worksheet
    error = CA_VariantSetCString (& MyCellRangeV, EXCEL_ARRAY_OF_CELLS);
    error = Excel_WorksheetRange (ExcelWorksheetHandle, NULL, MyCellRangeV, CA_DEFAULT_VAL, & ExcelRangeHandle);

    Make the range Active
    error = Excel_RangeActivate (ExcelRangeHandle, & ErrorInfo, NULL);

    Set column width
    error = Excel_SetProperty (ExcelRangeHandle, & ErrorInfo, Excel_RangeColumnWidth, CAVT_DOUBLE, 15.0);

    ..........
    }

    //-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Let us know if it works for you.

  • How to make a table with ActiveX Excel using Excel_RangeSetValue2?

    Hello

    I want to make a table in Excel. For this, I use two functions: Excel_WorksheetGetRange and Excel_RangeSetValue2.

    I write the table on Excel file, but only the first instance of the table is copied.

    Here, a part of my program:

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

    cCell Char [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 get when I'm compiling on Excel file:

    A

    A

    A

    A

    You must use the other instructions to complete 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 of data table
    CA_VariantSet2DArray (& MyVariant, CAVT_CSTRING, lines, 1 table);
    Transfer data to Excel range
    Excel_SetProperty (ExcelRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, MyVariant);

    ClearObjHandle (ExcelRangeHandle);

  • write data to the cell in excel

    Hello

    I am trying to write data to a cell in excel. When I'm looking in sample program, I felt a bit difficult to understand.

    I have written code to read data from a particular cell that works well. I am facing difficulties in writing data in the cell. pls help someone.

    to read a data cell that works

    status is Excel_RangeGetItem (rangeHandle, NULL, CA_VariantInt (i + 1), CA_VariantInt (j + 1), & MyVariant);.
             
    Get the pointer of SHIPPING
    status = CA_VariantGetDispatch (& MyVariant, & MyDispatch);
               
    Create a Range object pointer for SHIPPING
    status = CA_CreateObjHandleFromIDispatch (MyDispatch, 0, & ExcelSingleCellRangeHandle);
               
    Get the value of the range of simple cells
    status = Excel_GetProperty (ExcelSingleCellRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, & MyVariant);
              
    status = CA_VariantGetCString (& MyVariant, &val);)
    strcpy (CellValue, Val);

    to write data in a cell

    status is Excel_RangeSetItem (rangeHandle, NULL, CA_VariantInt (i + 1), CA_VariantInt (j + 1), MyVariant);.
             
    status = CA_VariantSetDispatch (& MyVariant, & MyDispatch);
               
    status = CA_CreateObjHandleFromIDispatch (MyDispatch, 0, & ExcelSingleCellRangeHandle);
       
    status = Excel_SetProperty (ExcelSingleCellRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, & MyVariant);
             
    status = CA_VariantSetCString (& MyVariant, writedata);

    I know I made a mistake in the code above.

    Thank you very much, Roberto. You have solved my problem.

  • CVI - Course excel files open "windows in.

    Hello

    I'm working on a project en CVI (8,0), who must deal with Excel files.

    The problem is that my software don't control the file which is open: its name and its location are not known normal program.

    My idea is to browse excel files open, to verify the presence of the characteristic for some characters in.

    Normal however, I can't seem to find the fonctionns allowing me to browse these files in the Excel API.

    My idea would be for example:

    Lancer Excel (se connecter, avec le Excel_ActiveApp )
    POUR chaque fichier ouvert FAIRE
         Lire contenu case 'A1'
         Si contenu == "toto" ALORS
            // C'est mon fichier
        SINON
            // C'est pas mon fichier
        FIN SI
    FIN POUR
    

    I tried searching with

    Microsoft Excel 14.0 object library

    and

    Microsoft Excel 9.0 Object Library

    I have not found.

    So I'm looking for help,

    Thanks in advance,

    Cédric.

    I find the solution here:

    http://www.theusenetarchive.com/Usenet-message-CVI-and-Excel-Help-11429335.htm

    This modified callback in excel2000dem sample project lists all opened files in Excel:
    
    //----------------------------------------------------------------------------
    // ConnectApp
    //----------------------------------------------------------------------------
    int CVICALLBACK ConnectApp (int panel, int control, int event,
    void *callbackData, int eventData1, int eventData2)
    {
    long ExcelWorkbooksCount;
    HRESULT error = 0;
    int i;
    char *ExcelWorkbookName;
    
    switch (event)
    {
    case EVENT_COMMIT:
    // Launch App
    // Connect to existing application if available
    SetWaitCursor (1);
    error = Excel_ActiveApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle);
    SetWaitCursor (0);
    if (error<0)
    goto Error;
    
    // Make App Visible
    error = Excel_SetProperty (ExcelAppHandle, NULL, Excel_AppVisible, CAVT_BOOL, appVisible?VTRUE:VFALSE);
    if (error<0)
    goto Error;
    
    // Get number of open files
    error = Excel_GetProperty (ExcelAppHandle, NULL, Excel_AppWorkbooks, CAVT_OBJHANDLE, &ExcelWorkbooksHandle);
    if (error < 0) goto Error;
    error = Excel_GetProperty (ExcelWorkbooksHandle, NULL, Excel_WorkbooksCount, CAVT_LONG, &ExcelWorkbooksCount);
    if (error < 0) goto Error;
    
    // Iterate through files and get file name
    for (i = 1; i <= ExcelWorkbooksCount; i++) {
    error = Excel_WorkbooksItem (ExcelWorkbooksHandle, NULL, CA_VariantInt(i), &ExcelWorkbookHandle);
    if (error<0)
    goto Error;
    error = Excel_GetProperty (ExcelWorkbookHandle, NULL, Excel_WorkbookName, CAVT_CSTRING, &ExcelWorkbookName);
    // Print file name
    DebugPrintf ("Workbook %d: %s\n", i, ExcelWorkbookName);
    // Free resources
    ClearObjHandle (&ExcelWorkbookHandle);
    CA_FreeMemory (ExcelWorkbookName);
    if (error<0)
    goto Error;
    }
    
    UpdateUIRDimming(panelHandle);
    MakeApplicationActive ();
    excelLaunched = 0;
    break;
    }
    return 0;
    Error:
    if (error < 0)
    ReportAppAutomationError (error);
    
    return 0;
    }
    
  • CVI and Excel Help?

    Hi all

    Is there a way to write to an Excel sheet spread during this same worksheet is open with windows Explorer?

    Something like a shared connection.

    Kind regards

    Shako

    This put reminder to update the sample excel2000dem project lists all open files in Excel:

    //----------------------------------------------------------------------------
    // ConnectApp
    //----------------------------------------------------------------------------
    int CVICALLBACK ConnectApp (int panel, int control, int event,
            void *callbackData, int eventData1, int eventData2)
    {
        long    ExcelWorkbooksCount;
        HRESULT error = 0;
        int     i;
        char    *ExcelWorkbookName;
    
        switch (event)
            {
            case EVENT_COMMIT:
                // Launch App
                // Connect to existing application if available
                SetWaitCursor (1);
                error = Excel_ActiveApp (NULL, 1, LOCALE_NEUTRAL, 0, &ExcelAppHandle);
                SetWaitCursor (0);
                if (error<0)
                    goto Error;
    
                // Make App Visible
                error = Excel_SetProperty (ExcelAppHandle, NULL, Excel_AppVisible, CAVT_BOOL, appVisible?VTRUE:VFALSE);
                if (error<0)
                    goto Error;
    
            // Get number of open files
            error = Excel_GetProperty (ExcelAppHandle, NULL, Excel_AppWorkbooks, CAVT_OBJHANDLE, &ExcelWorkbooksHandle);
                if (error < 0) goto Error;
                error = Excel_GetProperty (ExcelWorkbooksHandle, NULL, Excel_WorkbooksCount, CAVT_LONG, &ExcelWorkbooksCount);
                if (error < 0) goto Error;
    
            // Iterate through files and get file name
            for (i = 1; i <= ExcelWorkbooksCount; i++) {
            error = Excel_WorkbooksItem (ExcelWorkbooksHandle, NULL, CA_VariantInt(i), &ExcelWorkbookHandle);
                if (error<0)
                        goto Error;
            error = Excel_GetProperty (ExcelWorkbookHandle, NULL, Excel_WorkbookName, CAVT_CSTRING, &ExcelWorkbookName);
                    // Print file name        DebugPrintf ("Workbook %d: %s\n", i, ExcelWorkbookName);                // Free resources                ClearObjHandle (&ExcelWorkbookHandle);
            CA_FreeMemory (ExcelWorkbookName);
                    if (error<0)
                        goto Error;
            }
    
                UpdateUIRDimming(panelHandle);
                MakeApplicationActive ();
                excelLaunched = 0;
                break;
            }
        return 0;
    Error:
        if (error < 0)
            ReportAppAutomationError (error);
    
        return 0;
    }
    
  • FormatConditions for Excel in CVI

    Hey everone!

    I am writing a program in CVI which reads data from ASCII files and after a few calculations print an Excel worksheet. I do this with Excel report instrument and it works so far.

    Now, I want the cells to be formatted, according to other cell values. Let's say I have cells with the following values in the worksheet:

    Max: 5

    min: 3

    value: 2

    The State of the cell, containing the 2 should be: if (larger than max, or smaller than min)-> red, or green

    I found a function in the Library Microsoft Excel 9.0, named Excel_FormatConditionsAdd, but I don't know how to use it.

    The Panel is fixed.

    And there seems to be no help file.

    Could someone give me a hint on how to use this function? I'd also appreciate links to useful resources...

    Hope there's someone out there...

    see you soon

    Wohoo...

    I finally found the solution, I really had to do the opposite:

    //first get the FormatConditions property of the desired range
    Excel_GetProperty (ExcelRangeHandle, NULL, Excel_RangeFormatConditions,CAVT_OBJHANDLE, &ExcelFMs);
    
    //THEN add the conditions
    Excel_FormatConditionsAdd (ExcelFMs, NULL, ExcelConst_xlCellValue, CA_VariantInt(ExcelConst_xlGreater), MyVariant, CA_DEFAULT_VAL, &ExcelFM);
    
    //Now get the FormatConditionInterior property of the FormatCondition
    Excel_GetProperty(ExcelFM,NULL,Excel_FormatConditionInterior,CAVT_OBJHANDLE,&interiorHandle);
    //and finally set the InteriorColorIndex property of this thing to some number (3 is red)
    Excel_SetProperty(interiorHandle,NULL,Excel_InteriorColorIndex,CAVT_LONG,3);
    

    After this terrible pain in the ass I really deserve a beer now.

    Thank you again once, first I thought that this thing would never work.

Maybe you are looking for