Save An Excel File Into a Tab Delimited File

I am trying to save an Excel tile into a tab delimited TXT file. But I keep on getting an error message when I try the script below. Any suggestions?

tell application “Finder”
set vSavePath to (path to desktop folder as string) & “My Saved Workbook 9.txt”
end tell
tell application “Microsoft Excel”
tell active workbook
save workbook as filename vSavePath file format text Mac file format
end tell
end tell

I think one problem with your script is that you’re doing a “save workbook” inside a “tell workbook” statement. The workbook probably doesn’t know how to save the workbook, that command should be addressed to the application directly.

Beyond that… I actually got rather confused myself trying to get the syntax right looking at the Excel dictionary, and only actually got it working by Googling up someone else’s functional example… and I still find the working syntax rather odd.

set vSavePath to (path to desktop folder as string) & "My Saved Workbook 9.txt"

tell application "Microsoft Excel"
	set workbookName to get full name of active workbook
	save workbook as workbook workbookName filename vSavePath file format text Mac file format
end tell

Incidentally, you didn’t need to “tell application Finder,” the command “path to desktop folder” does not require it.

Please use the Applescript tags for your code when posting.

  • Tom.

Thank you for responding to my posting.

I copied exactly what you suggested, but it still gave me an error:

“Microsoft Excel got an error. The object you are trying to access does not exist.”

And from the Script Editor:
error “Microsoft Excel got an error: The object you are trying to access does not exist” number -1728 from workbook “/Users/jmendoza/Documents/Projects/CHE Projects/CHE Liners/My Saved Workbook 10.xlsx”

I did check the path and the file is there.

*note: I am new at posting here. Forgive me. What do you mean by “Please use the Applescript tags for your code when posting.”

May you try to run :

set vSavePath to (path to desktop folder as string) & "My Saved Workbook 9.txt"

close access (open for access file vSavePath) # ADDED

tell application "Microsoft Excel"
	set workbookName to get full name of active workbook
	save workbook as workbook workbookName filename vSavePath file format text Mac file format
end tell

The added instruction is supposed to get rid of a “well known” bug of not too old systems.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 24 mai 2017 18:34:51

Thank you Yvan.

But I am still getting the same error message even with that new added line.

May it be a conflict between the name of the Excel file : “My Saved Workbook 10.xlsx”
and the name used for the text file “My Saved Workbook 9.txt” ?

If it’s that, it would be useful to edit the script as :

tell application "Microsoft Excel"
	set workbookName to get full name of active workbook
	--> "/Users/jmendoza/Documents/Projects/CHE Projects/CHE Liners/My Saved Workbook 10.xlsx"
end tell
tell application "System Events"
	set HFSpath to path of disk item workbookName
	--> "Macintosh HD:Users:jmendoza:Documents:Projects:CHE Projects:CHE Liners:My Saved Workbook 10.xlsx"
end tell
set HFSalias to HFSpath as alias
tell application "Finder"
	set {itsName, itsExtension} to {name of HFSalias, name extension of HFSalias}
	--> {"My Saved Workbook 10.xlsx", "xlsx"}
end tell

if itsExtension is in {"", missing value} then
	set bareName to itsName
else
	set bareName to text 1 thru -(2 + (count itsExtension)) of itsName
	--> "My Saved Workbook 10"
end if

set vSavePath to (path to desktop folder as string) & bareName & ".txt"
--> "Macintosh HD:Users:jmendoza:Desktop:My Saved Workbook 10.txt"


close access (open for access file vSavePath) # ADDED

tell application "Microsoft Excel"
	save workbook as workbook workbookName filename vSavePath file format text Mac file format
end tell

I don’t own Merdosoft products so I can’t test.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 24 mai 2017 19:41:44

Grumble grumble… when I was testing, I hadn’t saved the open file, I just made an unsaved document and started scripting, and my script worked fine to export the txt file. Since it didn’t work for you, I tried the case of first saving my test document to the desktop as “My Saved Workbook 10.xls.” And sure enough, my script broke.

This worked:

set vSavePath to (path to desktop folder as string) & "My Saved Workbook 9.txt"

tell application "Microsoft Excel"
	set currentWorkbook to the active workbook
	save workbook as currentWorkbook filename vSavePath file format text Mac file format
end tell

Let me know if that works for you.

Also, what version of Excel are you running?

Yvan and T.Spoon,

Thank you for your time, patience and assistance.

Yvan, I haven’t tried your suggestion yet. But T.Spoon’s latest posting worked! Much appreciated to both of you.

I was using Excel version 2016, which is part of Office 365.

Note: I’ve done AppleScript for Adobe InDesign, but this is the fist time that I am doing it for Excel.

T.Spoon,

On this line on the script you suggested:

set vSavePath to (path to desktop folder as string) & “My Saved Workbook 9.txt”

It assumes that I have that exact file with that name located at desktop open.

What if I have any Excel file open, then I want to save it using a new different file name, as a text file to the desktop? And after it is saved using the new name, how do I close it without any new dialog boxes?

That is not how it’s working when I run the script.

That is simply generating the path location and name you wish to use to save the file. It has nothing to do with the name and save location of the currently open file.

If you want it to be something else, let me know how you want the name/location to be generated / or user entered and I can change it.

T.Spoon,

For some reason, I was getting errors earlier unless it was the same filename and location.

But I quit on Excel and Scripter, and opened them again. It’s working on any open file just as you said.

Thanks again.

Thanks for the feedback.

I was unable to guess that the problem was with the syntax of the instruction exporting the document.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 25 mai 2017 12:43:11