Search code examples
excelcomtcl

Unknown error 0x800A01A8 when trying to edit xsl file via TCL (tcom)


What I am trying to do is to add a text to some excel file, using TCL script . For working with excel I am using Tcom .

set excelFilePath "C:/TCL.Marko.xlsx"

proc start_tcom {} {
    global excelFilePath excelApp workbook 
    set excelApp [::tcom::ref createobject Excel.Application]
    set workbooks [$excelApp Workbooks]
    set workbook [$workbooks Open [file nativename [file join [pwd] $excelFilePath] ] ]
    set worksheets [$workbook Worksheets]
    #
    set excel_sheet Marko.xlsx
    set worksheet [$worksheets Item [expr 1]]
    set cells [$worksheet Cells]
    return $cells
}

Up till this point all works.

509 % start_tcom
::tcom::handle0x0284F9D8

Then I am running:

513 % foreach row {1 2 3} {
    foreach column {A B C} {
        $cells Item $row $column [incr i]
    }
}

and getting this error:

0x800a01a8 {Unknown error 0x800A01A8}

if I am doing save_and_close_tcom I can see file Marko.xslx is updated with the correct time and date, so the TCL see the correct file, but still getting this error.


Solution

  • the " set excel_sheet Marko.xlsx" should be removed . this command means working with tab name "Marko.xlsx" , but this tab doesnt exist. and thats why the error came. this is the correct code with explanations :

    proc start_tcom {} {

                global excelFilePath excelApp workbook 
                  set excelApp [::tcom::ref createobject Excel.Application]
                 # Connect to Excel COM object.
    
                set workbooks [$excelApp Workbooks]
                set workbook [$workbooks Open [file nativename [file join [pwd] $excelFilePath] ] ]
                set worksheets [$workbook Worksheets]
                # Create inital workbook.
    
    
               set worksheet [$worksheets Item [expr 1]]
             $worksheet Name "Booleans"
      # Rename the first tab.
    
                set cells [$worksheet Cells]
                return $cells
                #Making cells ready for input
    

    }

    Now we ready to work directly with the XSL document

    I’ll show an example of copying a simple txt file in to the XSL we just created .

    Lets assume we have a TXT document named WTX.txt with a list of values.

    set fp [open "[pwd]/TEMP/WTX.txt" r] set file_data [read $fp]

    Now as we know each cell in XSL is indexed by a column and raw , so in order to populate the XSL we need to specify the exact indexes.

                set Col A 
                set Row 1 
    

    in the following code we’re creating a simple while loop to run over the txt file values and populate the XSL with them .

                set Row 1
                while {[lindex $file_data $i] != ""} {
                $cells Item $Row $Col $numeric_value_from_file
                incr Row
    

    }

    lets discuss the marked line , this is the actual command to populate the cell with the value.

    The actual format of the line is : $cells (we created it previously) ITEM (the cell it self) Row Col

    So what we actually saying is (backwords) Take the Value and put it to cell indexed with some Row and Col.

    EOF.