copy from a workbook to anaother with applescript

D

degouville

Version: 2008
Operating System: Mac OS X 10.5 (Leopard)
Processor: Intel

Hello,
Can someone explain me why this script does not work ?
It does not want to active the target workbook. So it copies the values in the source workbook and not in the target one.

tell application "Microsoft Excel"
activate
set source_file to "Macintosh HD:Users:philippedegouville:Documents:projet:script:source.xls"
set target_file to "Macintosh HD:Users:philippedegouville:Documents:projet:Script:cible.xls"
open source_file
open target_file

repeat with i from 1 to 3
activate object workbook source_file
set valeur_source to value of cell i of column 1
activate object workbook target_file
set value of cell (i + 3) of column 1 to valeur_source
end repeat
end tell

Thanks
 
J

JE McGimpsey

Version: 2008
Operating System: Mac OS X 10.5 (Leopard)
Processor: Intel

Hello,
Can someone explain me why this script does not work ?
It does not want to active the target workbook. So it copies the values in
the source workbook and not in the target one.

One problem is that workbooks don't have cells, worksheets do...

Also, while it's certainly not wrong, in this application there's no
efficiency gained by using an intermediate variable.

An alternative:

set pathName to ¬
"Macintosh HD:Users:philippedegouville:Documents:projet:script:"
set sourceBook to "source.xls"
set targetBook to "cible.xls"

tell application "Microsoft Excel"
open pathName & sourceBook
open pathName & targetBook

set sourceSheet to worksheet 1 of workbook sourceBook
set targetSheet to worksheet 1 of workbook targetBook

repeat with i from 1 to 3
set value of cell (i + 3) of column 1 of targetSheet to ¬
(get value of cell i of column 1 of sourceSheet)
end repeat

end tell

or do it without the loop:

set sourceRange to get resize cell 1 of column 1 of ¬
worksheet 1 of workbook sourceName row size 3

set targetRange to get resize cell 4 of column 1 of ¬
worksheet 1 of workbook targetName row size 3

set value of targetRange to (get value of sourceRange)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top