Home » excel » excel – AppleScript local variable not found

excel – AppleScript local variable not found

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have this AppleScript which has been working for years:

-- excerpted portion of larger script
tell application "Microsoft Excel"
  set jfFileName to "My Finances " & yearChoice & ".xls"
  try
    set theWorkbook to workbook jfFileName
  on error
    display dialog "Sorry, Excel doesn't seem to have the file '" & jfFileName & "' open. Please open it, and run again." buttons {"OK"} default button "OK"
    error -128
  end try
  activate object worksheet "IncomingValues" of theWorkbook
end tell

I upgraded to Mavericks last night and have multiple problems with this script.

  1. The next to last line (in the first code snippet) now shows a syntax error, Expected end of line, etc. but found class name with the text worksheet highlighted. I delete the word “object” and it now compiles to «event XCELactv» «class cXLW» "IncomingValues" of theWorkbook
  2. When I run the script here is the result:
tell application "Microsoft Excel"
  get «class WKBK» "My Finances 2012.xls"
    --> current application
Result:
  error "The variable theWorkbook is not defined." number -2753 from "theWorkbook"

How do I fix this? Is there an AppleScript variable scoping change in Mavericks?

(The problem persists if I move the set outside of the try block.)

How to&Answers:

Update: The OP reports that the following ultimately solved his problem:

  • Cleaning up the list of dictionaries first, using /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.fra‌​mework/Support/lsregister -kill -r -domain local -domain system -domain user

  • Opening a clean, uncompiled plain-text version of the script (one that didn’t have values such as «event XCELactv» in it), and recompiling it.


The following is based on Excel v14.3.9 on OS X 10.9.1:

Your symptoms do sound like there’s a problem with the Microsoft Excel.app scripting dictionary, especially given your statement that another Mavericks machine doesn’t exhibit the problem.

From the AppleScript Language guide:

AppleScript uses double angle brackets [chevrons, i.e. «»] in a Script Editor script window when it can’t identify a term. That happens when it encounters a term that […] isn’t defined in an application or scripting addition dictionary that is available when the script is opened or compiled.

Is it possible that you have an OLDER version of the Microsoft Excel dictionary installed?
On my machine, where your code works, I see the following location in the list of installed dictionaries: /Applications/Microsoft Office 2011/Microsoft Excel.app.
Do you see that too?
Do you see more than one Excel-related dictionary listed? Make sure the above-mentioned one is the only one.

You can also try rebuilding the launch-services database to clean up the list of dictionaries (no idea if that really helps):

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user 

If that doesn’t help, try reinstalling Microsoft Office (a pain, but worth a try).

As for your current code and what you’ve tried:

  • Even when the script works as written, your error handler for when the target workbook is not currently open doesn’t kick in – Excel apparently quietly sets theWorkbook to missing value – this may explain the "The variable theWorkbook is not defined." error.

If you wanted to make absolutely sure that you don’t proceed unless you have a valid theWorkbook reference, use the following:

set theWorkbook to missing value
try
    set theWorkbook to workbook jfFileName
end try
if theWorkbook is missing value then
    set errMsg to "Sorry, Excel doesn't seem to have the file '" & jfFileName & "' open. Please open it, and run again."
    display alert errMsg
    error errMsg number -128
end if
  • Based on the Microsoft Excel.app dictionary you DO need the object after activate to activate a workbook.

otherwise, anything after the activate is quietly ignored and the Excel application as a whole will activate, with whatever workbook and sheet is currently active there; by contrast, the proper activate object worksheet ... statement will only activate the targeted worksheet’s window and the worksheet itself within the application, without activating the application itself.

The Expected end of line, etc. but found class name error message suggests that the Excel-specific activate object someObj syntax is not recognized – pointing again to a dictionary problem.

Answer:

You can try something like this:

set yearChoice to "2012" -- testing
set jfFileName to "My Finances " & yearChoice & ".xls"

tell application "Microsoft Excel"
    set openWorkbooks to workbooks
    repeat with aWorkbook in openWorkbooks
        if aWorkbook's name = jfFileName then
            set theWorkbook to (contents of aWorkbook)
            exit repeat
        end if
    end repeat

    try
        activate object worksheet "IncomingValues" of theWorkbook
        -- insert your code here
    on error
        display dialog "Sorry, Excel doesn't seem to have the file '" & jfFileName & "' open. Please open it, and run again." buttons {"OK"} default button "OK" cancel button "OK"
    end try
end tell

Answer:

You code works fine on my machine with Excel v14.3.9 on OS X 10.9.2 beta 2

You may try reinstalling the Office Suite.

Your code however have a bug: the try-catch block is not working as you expect if the workbook is not open.

You can correct like this:

tell application "Microsoft Excel"
    set jfFileName to "My Finances " & yearChoice & ".xls"
    set theWorkbook to workbook jfFileName
    if not ( workbooks contains theWorkbook ) then
        display dialog "Sorry, Excel doesn't seem to have the file '" & jfFileName & "' open. Please open it, and run again." buttons {"OK"} default button "OK"
        error -128
    end if
    activate object worksheet "IncomingValues" of theWorkbook
end tell