workbooks mix up

Hi,
I try this rather simple piece of code and it react strangely :

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”

set source_book to open workbook workbook file name source_file
set source_sheet to sheet "feuille source"

set target_book to open workbook workbook file name target_file
set target_sheet to sheet "feuille cible"

set value of cell 2 of row 1 of source_sheet to 2000

end tell

I opened two workbooks and create two sheet objects. But when I try to set the value of a cell in “source sheet”. It sets the value in a cell of the sheet of the last opened workbook.
If I switch the workbooks, it works but I still cannot affect a value to the worksheet of the first workbook. Am I missing something ?

Thks

Hi,

it’s probably again a referencing problem.
sheet without any reference is always a sheet of the active workbook.
Try these specified references


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"
	
	set source_book to open workbook workbook file name source_file
	set source_sheet to sheet "feuille source" of source_book
	
	set target_book to open workbook workbook file name target_file
	set target_sheet to sheet "feuille cible" of target_book
	
	set value of cell 2 of row 1 of source_sheet to 2000
	
end tell

Hi SefanK,

It works great and it means that AS does not link the sheet object automatically with the active workbook and you have to specify completly the instance you want to create.

Pongocito

Yes, AS links the sheet object dynamically with the active workbook if no further reference is specified.
In your origin code source_sheet is first a sheet of workbook “source”.
After opening the workbook “cible” this workbook becomes active and the reference of source_sheet changes to sheet of workbook “cible”

If you work with multiple workbooks it’s recommended to use full “paths”

Understood. Thks for your help.

Regards.