Applescript in Excel 2004

A

Alan Schmidt

The following applescript works just fine in Excel X but not in Excel 2004.

tell application "Microsoft Excel"
Set ScreenUpdating to False
OpenText "MyComputer:Applications:MyApp:Customer Contacts"
CopyObject Sheet "Customer Contacts"
Set ScreenUpdating to True
Close Window "Customer Contacts"
activate
end tell

From the 2004 dictionary looks like OpenText and CopyObject Sheet are no
longer supported??? How would I replace them? Any pointers would be greatly
appreciated. TIA!
--
Alan Schmidt
Bottom Line Software, Inc.
PO Box 20997, Bakersfield CA 93390-0997
phone 661-399-2662 fax 661-399-2772
email (e-mail address removed)
web www.goconnected.com
 
P

Paul Berkowitz

The following applescript works just fine in Excel X but not in Excel 2004.

tell application "Microsoft Excel"
Set ScreenUpdating to False
OpenText "MyComputer:Applications:MyApp:Customer Contacts"
CopyObject Sheet "Customer Contacts"
Set ScreenUpdating to True
Close Window "Customer Contacts"
activate
end tell

From the 2004 dictionary looks like OpenText and CopyObject Sheet are no
longer supported??? How would I replace them? Any pointers would be greatly
appreciated. TIA!

Maybe you could spend a bit of time browsing the dictionary? 'screen
updating' is (still) a property of 'application'. There's a separate 'copy
worksheet' command - listed just below the 'copy object' command - which now
copies sheets. And there's both an 'open' and 'open text file' command.
Since you're not using any special parameters for text files, I would think
that 'open' should be good enough. ('open' is in the Standard Suite, 'open
text file' in the Microsoft Excel Suite.) Especially since 'open text file'
presumes it's a (tab or comma separated, usually) text file which will have
only one sheet.


tell application "Microsoft Excel"
set screen updating to false
open "MyComputer:Applications:MyApp:Customer Contacts"
copy worksheet sheet "Customer Contacts" -- of active workbook
set screen updating to true
close window "Customer Contacts"
activate
end tell


It's not so hard as all that, is it? If the file really is a text file that
won't respond to 'open', use 'open text file', nut you may need to specify
some of the optional parameters you'll find for that command if they're not
all defaults.

The script above not only copies the sheet to clipboard but seems to paste
it into a new "Workbook1" (or Workbook2, etc.) unsaved workbook with a
single "Customer Contacts" sheet, open in front when Excel activates at the
end. I'm not sure if that's what you want. The original file has closed.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
A

Alan Schmidt

Hi Paul,

Thanks for your (slightly stinging) reply. I had looked through the
dictionary and tried the commands you suggested but kept getting the errors:
"Microsoft Excel got an error: "MyComputer:test:Customer Contacts" doesn't
understand the open message." or "Microsoft Excel got an error: sheet
"Customer Contacts" doesn't understand the copy worksheet message." So I
assumed I was missing something- Applescripting is not one of my strengths,
but the same thing happens with your script.

I'm trying to save a few bucks (which always backfires) by running this on
the Office 2004 test Drive that was preinstalled on my iMac G5- has
Applescript support been removed from that version?

Thanks for your help.
--
Alan Schmidt
Bottom Line Software, Inc.
PO Box 20997, Bakersfield CA 93390-0997
phone 661-399-2662 fax 661-399-2772
email (e-mail address removed)
web www.goconnected.com
 
P

Paul Berkowitz

I tested my own script and it worked fine. So I expect that it's just a
simple matter of having the file path wrong. There could be two different
reasons:

1) Is the path _really_

"MyComputer:test:Customer Contacts"

That implies that you made a folder called "test" on the root of your
startup disk called "MyComputer". Maybe it's really in your own user folder

"MyComputer:Users:alan:test:Customer Contacts"

2) More likely is that you're omitting an extension at the end of the file
name. There are very few files on the Mac made by standard applications (or
brought over from Windows) which don't have extensions. But they may be
hidden. So first of all go to the Finder, go to Finder/Preferences/Advanced.
and check "Show all file extensions".

Then, your "Customer Contacts" file will show an extension if it has one. If
the extension is a regular Excel extension such as .xls, or if it's .csv, or
if it's .txt and the file is actually a tab-delimited file, 'open' will work
.. I tested it with a standard tab-delimited text file with .txt extension.
If it has any unusual delimiters or other non-default properties, you'd need
to use 'open text file' and use whichever parameters are needed.

You still haven't said a word about what type of file it is, so there's no
way to help you unless you provide some details. But the fact that you used
to be able to use OpenText with no parameters indicates to me that it's just
a problem with the file path, and that as soon as you get that right it will
probably work OK.

Unlike the Test Drive for Office v. X, which did have both AppleScript and
VBA stripped out, the Test Drive for Office 2004 has full-featured
AppleScript. Id it didn't, the script wouldn't even compile.


--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
A

Alan Schmidt

Okay Paul you got it- there was a txt extension. After fixing that it runs
fine. Now, since the scripts are slightly different, is there some way I can
determine which version of Excel- 2004 or X that will be used when the
script is run?

Thanks for your help and patience!
--
Alan Schmidt
Bottom Line Software, Inc.
PO Box 20997, Bakersfield CA 93390-0997
phone 661-399-2662 fax 661-399-2772
email (e-mail address removed)
web www.goconnected.com
 
P

Paul Berkowitz

Okay Paul you got it- there was a txt extension. After fixing that it runs
fine.
Great!

Now, since the scripts are slightly different, is there some way I can
determine which version of Excel- 2004 or X that will be used when the
script is run?

It might be a bit more complicate than that, to have a script that can be
compiled for either version. Can you explain what you mean by "determine"?

Do you mean that you want the one script to be able to work with both Excel
X and 2004, depending on what version the end user has? That is possible,
but a bit complicated. Can you really not make two versions, and ask the end
user to pick the script that applies to his/her version of Excel? Or do you
mean something else?

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
A

Alan Schmidt

In our app, the end user exports a file and it's then automatically opened
in Excel. I'd prefer being able to do this without any end user
intervention/configuration. So if the script was able to work for either X
or 2004 that would be dynamite!

Thanks again Paul.
--
Alan Schmidt
Bottom Line Software, Inc.
PO Box 20997, Bakersfield CA 93390-0997
phone 661-399-2662 fax 661-399-2772
email (e-mail address removed)
web www.goconnected.com
 
P

Paul Berkowitz

In our app, the end user exports a file and it's then automatically opened
in Excel. I'd prefer being able to do this without any end user
intervention/configuration. So if the script was able to work for either X
or 2004 that would be dynamite!

After opening Excel 2004 and your Script Editor, open the compiled script
(or script application) - NOT a text-only version - that you used to use in
Excel X or earlier. You'll see that most of the old commands in Excel tell
blocks have been replaces by raw codes, like this:

set ur to «class PURG» of «class PASH»
«event XCELBSFT» every column of ur

That's fine. (If this is a NEW script which doesn't yet exist in an Excel X
version, you need to quit Script Editor, quit Excel 2004, log out (Apple
menu) log in, open Excl X, open Script Editor, and write your script. To
"transfer" an Excel X script to Excel 2004 or vice versa, first save a copy
of the script As Text. Then after doing the log in/out stuff, open the text
(.applescript or .txt) version while in the other Excel. That way you see
the English-language terminology which you replace with the new version. But
that's NOT what we're doing here. Here I'm assuming you already have both
versions of the script, and you should open the compiled X script while 2004
is open, to see the «raw codes».)

In your 2004 script, preface this to the very top (under any property and
global declarations you might have):


tell application "Microsoft Excel" to set theVersion to version as
string

set AppleScript's text item delimiters to {"."}
set mainVersion to (text item 1 of theVersion) as integer
set AppleScript's text item delimiters to {""}
if mainVersion < 11 then
-- paste your entire X version of the script, complete with «raw
codes»

else
-- continue with entire 2004 version

end if



That should compile OK, and run in either version. It will work best if the
user opens Excel first, before running the script. It should even work for
you with either version of Excel open. BUT - if you want to run it from
Script Editor, you'll first have to quit it and log out/in, open Excel and
Script Editor before switching to the version you have not been using
already.

If you're script is long, you might prefer to put each version on a handler
and just call the appropriate handler in each branch of the if/else block
above: it makes no difference.

Things could get peculiar if you try to run the script when you have more
than one version of Excel on your computer and you don't have either one
open. It shouldn't be a problem if the user has only one Excel. But it's
smoothest to open an Excel before running the script.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
A

Alan Schmidt

Hey Paul,

Sorry to have taken so long to thank you for your help. Your advice helped
me find a solution to this. You're always a big help! Thanks again!!
--
Alan Schmidt
Bottom Line Software, Inc.
PO Box 20997, Bakersfield CA 93390-0997
phone 661-399-2662 fax 661-399-2772
email (e-mail address removed)
web www.goconnected.com
 

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