Search code examples
excelapplescriptosx-snow-leopardautomator

Here is some Applescript code to interact with Microsoft Excel on Snow Leopard - How to add the missing functions


I have about 400 excel files. I want to insert a column just before the first column in the existing file and then insert the name of the file into each row of that column.

I know a little bit of Applescript and based on that I wrote this script so that I can drop some files onto the script and it will execute the script on each one of those files.

I was wondering if someone could help me in completing the "TO DO" lines. Upon execution this script gives me dialogue boxes with the path of files that I drop on top. But the excel application throws an error dialogue box which says" Not enough memory". I tried this with only 2 excel files so it wasn't the number of files that caused the error.

Can someone please give me a hand with completing the TODO lines and give me an ide as to why Im getting the error. Thanks

property numFiles : 0

on open excelFiles


set fileNames to ""

tell application "Finder"
    repeat with eachFile in excelFiles

        --open document file eachFile

        --tell application "Microsoft Excel"

        --increment count



        --save name of each file

        set fileNames to fileNames & return & (POSIX path of eachFile)

        --TO DO insert a column

        --TO DO insert text in each column to the name of eachFile

        --end tell
    end repeat
    display dialog fileNames
    --display dialog "Ouch that hurt " & return & "You dropped " & (count excelFiles) & "files on me"
end tell
end open

on addFilePath(eachFile)
set fileNames to fileNames & (POSIX path of eachFile)
end addFilePath

Thanks a lot


Solution

  • I don't understand everything --> insert the name of the file into each row of that column | TO DO insert text in each column to the name of eachFile.

    Here is the script, Updated :

        on open excelFiles
        set numFiles to count excelFiles
        repeat with eachFile in excelFiles -- open each file in Excel
            tell application "Microsoft Excel"
                set tBook to open workbook workbook file name (eachFile as string)
                set tName to name of tBook
                insert into range column 1 of active sheet -- insert column
    
                set lastCell to last cell of used range of active sheet -- get last cell from the used range
                set value of range ("A1:A" & first row index of lastCell) of active sheet to tName --set first column's values to the file name
    
                close tBook saving yes
            end tell
        end repeat
        display dialog numFiles
    end open
    

    Edit : I forgot the error :

    Not enough memory : This weird error seems to be : you call a handler without using my or tell me to) in a tell block application.

    Use my like this : set x to my addFilePath(eachFile)

    Also, a tell application "Microsoft Excel" block in the application Finder block is not recommended, this can cause unexpected errors.