There are two ways to open an existing workbook. All applications implement the Standard Suite's open command, which is one of the most basic commands there is. The open command is required of all Macintosh applications, even those that are otherwise unscriptable. Actually, open, run, print, and quit are all required. Sometimes applications add their own specialized parameters to open, as Microsoft Word does.
However, to keep open as flexible as possible, since it can also be used both by unscriptable applications and by the Finder to open any application's documents without having any conception how to represent them when opened, it does not return a result. That is problematic for scriptable applications that open their own documents.
Microsoft Excel has a good solution: It leaves the Standard Suite's open command in place, where it can be used to open entire lists of files at once, but with no special parameters. Instead, it also has a separate open workbook command in the Excel Suite, equivalent to Visual Basic for Applications (VBA)'s Open, with all of its parameters (arguments), and which does return a result. You can therefore set a variable (reference) to the open workbook command. That's the best choice.
Workbooks.Open Filename:="Mac HD:Folder:File.xls"
translates to:
tell application "Microsoft Excel" open alias "Mac HD:Folder:File.xls" -- or: open "Mac HD:Folder:File.xls" -- or: open workbook workbook file name "Mac HD:Folder:File.xls" end tell
while
Set oWkbk = Workbooks.Open(Filename:="Mac HD:Folder:File.xls")
translates to:
tell application "Microsoft Excel" set theWkbk to open workbook ¬ workbook file name "Mac HD:Folder:File.xls" end tell
The open workbook version offers many options. To open a text file, you might find a macro with the Format argument that specifies the delimiter (1=Tab, 2=Comma, 3=Space, and so on), as shown in the following example:
Workbooks.Open FileName:="Mac HD:Folder:File.txt", Format:=1
This translates to:
open workbook workbook file name ¬ "Mac HD:Folder:File.txt" format tab delimiter
Note that you can also use the numbers
1,
2,
3, etc. for the format parameter, and it works. So for translating from VBA macros, don't go to the trouble of opening Microsoft Visual Basic Editor
to scour the Object Browser and work out which number is equivalent to which parameter. Just use the same number in the script. (There are quite a few enumerations where you can do this.) But it's a lot more useful having the proper names when writing your own scripts from scratch.
For text files, there is a more powerful command: open text file. Your VBA macro might well be using the VBA equivalent, OpenText, so you should do the equivalent, as shown in the following example:
Workbooks.OpenText FileName:="Mac HD:Folder:File.txt", _ DataType:=xlDelimited, Tab:=True
This translates to:
tell application "Microsoft Excel" open text file filename ¬ "Mac HD:Folder:File.txt" data type delimited with tab end tell
Note that in AppleScript, when using the boolean command values true and false following a parameter name (tab true, tab false), true compiles as with, and false compiles as without. The values also precede the parameter name, as in without tab and with tab. Don't be alarmed when that first happens. You can type them either way.
A group of booleans all of the same value compile together like so:
tab true consecutive delimiter true
compiles as:
with tab and consecutive delimiter
The VBA OpenText command might come with some rather complex information in the optional FieldInfo argument:
Workbooks.OpenText FileName:="Mac HD:Folder:File.txt", _ DataType:=xlDelimited, Tab:=True, _ FieldInfo:=Array(Array(3, 9), Array(1, 2))
Without getting into exactly what that means here (it has to do with specifying the data types of specific columns), you can translate that FieldInfo value to:
open text file filename ¬
"Mac HD:Folder:File.txt" data type delimited ¬
field info {{3, 9}, {1, 2 }} with tabwhere the VBA arrays become lists in AppleScript. If you need to understand and not just transcribe them, or if there is a problem with that method, read up on field info in either the VBA Help or the Microsoft Excel 2004 AppleScript Reference
(http://download.microsoft.com/download/3/A/7/3A70FB4B-0C6A-43E3-AAB7-AC9166B25632/Excel2004AppleScriptRef.pdf), and do it the following way, as specified by the dictionary:
open text file filename ¬
"Mac HD:Folder:File.txt" data type delimited ¬
field info {{3, skip column}, {1, text format}} with tab

