Using Applescript to paste the current date into a range of Excel cells

applescriptms office

I have a simple script written that successfully saves the current date in the format I need. I also have saved a destination range in the active sheet. I just need syntax to paste/insert it into that range of cells.

tell application "Microsoft Excel"
    get active workbook
    tell sheet 1
        set ReportDate to do shell script "date '+%d/%m/%Y'"
        set range_value to value of used range
        set firstUnusedCellA to (count range_value)
        set DestinationRange to "G2:G" & (firstUnusedCellA)
        get ReportDate
        paste special on worksheet active sheet destination range DestinationRange

end tell

Best Answer

I think the code below speaks for itself. The do shell script command shouldn't be used inside another application tell block. Still I prefer a vanilla AppleScript solution as below which is fine to be used in any context in the script.

tell (current date) to set {_day, _month, _year} to {day, it's month, year}
set _day to text -2 thru -1 of ("00" & _day) -- add leading zeros if needed
set _month to text -2 thru -1 of ("00" & (_month as integer)) -- add leading zeros if needed
set _date to _day & "/" & _month & "/" & _year

tell application "Microsoft Excel"
    tell workbook 1
        tell worksheet 1
            set value of range "$A1:A10" to _date
        end tell
    end tell
end tell