CustomTables in Sheets and Drawings

Top  Previous  Next

This example C++ code shows how to create a custom table in an IDW Inventor drawing:


void CreateExampleTable (CComPtr<Sheet>& pSheet)


    wchar_t* ColNames[128] = {L"Column One",L"Column Two",L"Column Three"};


    // These contents are after the title row and the header row

    const int ikNumCols = 3 ; 

    const int ikNumRows = 5 ;  // 7 with header and column titles


    SAFEARRAY* pstrColumns = CreateSafeStringArray(ikNumCols, ColNames);


    CComPtr<CustomTables> pCustomTables  ;

    pSheet->get_CustomTables (&pCustomTables) ;


    // This is top left of the table. Sheet origin is bottom left.

    // In Europe this coordinate system is usually cm in a sheet

    CComPtr<Point2d> ptTable; // Create a 2d point



    CComVariant varEmpty ;   

    CComPtr<CustomTable> pCustomTable  ;

    pCustomTables->Add (CComBSTR(L"HOLES"),ptTable,ikNumCols,ikNumRows,&pstrColumns,

                        varEmpty, // the contents will be added later

                        varEmpty, // use the default widths

                        varEmpty, // use the default heights

                        varEmpty, // no more info

                        &pCustomTable) ; // table returned



     * Here we add contents by using put_Value in every cell


    for (UINT iRow = 1 ; iRow <= ikNumRows ; iRow++) { 

        // Get the row where we want to add data...

        CComPtr<Row> pRow  ;

        pCustomTable->Rows->get_Item (long(iRow),&pRow) ;


        for (UINT iCol = 1 ; iCol <= ikNumCols ; iCol++) {

            // Get the column of the row where we want to add data...

            CComPtr<Cell> pCell  ;

            pRow->get_Item (CComVariant(iCol),&pCell) ;


            // Demo example text...

            wchar_t szCellContents[_MAX_PATH] ;

            swprintf_s (szCellContents,L"R%d C%d",iRow,iCol) ;


            // Put the data in the cell...

            pCell->put_Value (CComBSTR (szCellContents)) ;




    SafeArrayDestroy (pstrColumns) ;



The resulting table looks like this:




The code does not include error checking, which you should add in your own versions.


If you want to specify column widths you need to pass in a VARIANT which is an array of doubles, see WidthsVariant below:


    VARIANT WidthsVariant ;

    double ColWidths[5] = {2.5,3.5,4.5,6.5,2.5} ;

    CreateDoubleVariantArray (WidthsVariant,ColWidths,5) ;


    CComVariant varEmpty ;   

    CComPtr<CustomTable> pCustomTable  ;

    HRESULT hRes = pCustomTables->Add (CComBSTR(L"Special Data"),



                                       varEmpty, // the contents will be added later

                                       WidthsVariant, // widths of columns

                                       varEmpty, // use the default heights

                                       varEmpty, // no more info

                                       &pCustomTable) ; // table returned





Positioning objects in a Sheet.




Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)