Rename Excel Worksheet in Office 365

in Excel 2011 a simple routine renamed a worksheet.

[AppleScript]
tell application “Microsoft Excel”
set name of active sheet to “First”
end tell
[/AppleScript]

Having switched to Office 365 that no longer works

It throws error 10006 and I cannot find anything to fix it. Using “Worksheet” and a number like this works.

[AppleScript]
tell application “Microsoft Excel”
set name of worksheet 1 to “First”
end tell
[/AppleScript]

But that requires you know the number of the worksheet whose name you want to change. I have a work around that solves the issue but it is incredibly clumsy.

[AppleScript]
tell application “Microsoft Excel”
set Allsheets to count every sheet of active workbook
set ActName to name of active sheet
set x to 1
repeat Allsheets times
set Tname to name of sheet x
if Tname = ActName then
exit repeat
end if
set x to x + 1
end repeat
set name of worksheet x to “First”
end tell
[/AppleScript]

I was hoping that either I am missing something or some one has a better solution.

Thanks

I know that in Excel 2011, active sheet means by default the sheet on top, in the active workbook. I know this because I am using Excel 2011. You need to check the Excel 365 dictionary. Most likely, it is not so smart and needs to explicitly specify the workbook. For example, like this:


tell application "Microsoft Excel"
	tell active workbook to set name of active sheet to "First"
end tell

NOTE: 1) I would always use this template as it is more universal.
2) I can’t test this propose because I have other Excel version. So, check yourself.

You know I thought I had tried every possibility. Could not find it in the dictionary so my search terms must have been incorrect.

Thank you very much I am glad I can reduce 18 lines to 1.:):slight_smile: