D
dg
Hello,
I just spent the better part of two hours trying to figure out
something that ought to be very simple, but, in Excel, isn't.
I have an Excel file with several worksheets. What I want is to save
some of the worksheets as separate CSV files in the same directory as
the source file. Each CSV file should be named as the original source
file plus an underscore plus the worksheet name.
Example: Excel file called Source1.xls contains 3 worksheets called
WS1, WS2, and WS3. What I want is to have three additional files with
names Source1_WS1.csv, Source1_WS2.csv, and Source1_WS3.csv in the
same directory as Source1.xls containing its respective worksheets (if
the name of the worksheet would be preserved it would be better, but
not essential).
Sounds simple, right? Well, no.
Here's what I came up with. To simplify, in a first step I will
attempt to save all worksheets as CSV files.
set theFile to choose file
tell application "Microsoft Excel"
activate
open theFile
set sheet_count to the count of sheets
repeat with i from 1 to sheet_count
my save_sheet(i)
end repeat
close active workbook without saving
end tell
on save_sheet(i)
tell application "Microsoft Excel"
set theSheet to worksheet i of active workbook
set sheet_name to name of sheet i
set fname to (full name of active workbook)
set fname to text 1 thru -5 of fname --remove file extension
set tname to fname & "_" & sheet_name & ".csv"
activate object sheet i
save as (sheet i) filename tname file format CSV
end tell
end save_sheet
Well, what happens is that Excel CHANGES THE NAME OF THE ENTIRE
WORKBOOK upon saving the first worksheet (the first worksheet also
gets RENAMED). WHY OH WHY????
Therefore, the name of the second worksheet also contains the first,
etc.
What I get is:
- files Source1_WS1.csv, Source1_WS1_WS2.csv, and
Source1_WS1_WS2_WS3.csv are created
- the first (and only the first) worksheet in the original file is
renamed to Source1_WD1 (with the name truncated after a certain number
of characters)
- the original file name is changed to Source1_WS1_WS2_WS3.csv
(the last two changes are undone by the "close without saving"
statement, so the original file is preserved)
Why does Excel rename the whole file when I ask to save a
worksheet????!? That is nonsense.
Any idea on how to achieve what I want?
Thanks.
I just spent the better part of two hours trying to figure out
something that ought to be very simple, but, in Excel, isn't.
I have an Excel file with several worksheets. What I want is to save
some of the worksheets as separate CSV files in the same directory as
the source file. Each CSV file should be named as the original source
file plus an underscore plus the worksheet name.
Example: Excel file called Source1.xls contains 3 worksheets called
WS1, WS2, and WS3. What I want is to have three additional files with
names Source1_WS1.csv, Source1_WS2.csv, and Source1_WS3.csv in the
same directory as Source1.xls containing its respective worksheets (if
the name of the worksheet would be preserved it would be better, but
not essential).
Sounds simple, right? Well, no.
Here's what I came up with. To simplify, in a first step I will
attempt to save all worksheets as CSV files.
set theFile to choose file
tell application "Microsoft Excel"
activate
open theFile
set sheet_count to the count of sheets
repeat with i from 1 to sheet_count
my save_sheet(i)
end repeat
close active workbook without saving
end tell
on save_sheet(i)
tell application "Microsoft Excel"
set theSheet to worksheet i of active workbook
set sheet_name to name of sheet i
set fname to (full name of active workbook)
set fname to text 1 thru -5 of fname --remove file extension
set tname to fname & "_" & sheet_name & ".csv"
activate object sheet i
save as (sheet i) filename tname file format CSV
end tell
end save_sheet
Well, what happens is that Excel CHANGES THE NAME OF THE ENTIRE
WORKBOOK upon saving the first worksheet (the first worksheet also
gets RENAMED). WHY OH WHY????
Therefore, the name of the second worksheet also contains the first,
etc.
What I get is:
- files Source1_WS1.csv, Source1_WS1_WS2.csv, and
Source1_WS1_WS2_WS3.csv are created
- the first (and only the first) worksheet in the original file is
renamed to Source1_WD1 (with the name truncated after a certain number
of characters)
- the original file name is changed to Source1_WS1_WS2_WS3.csv
(the last two changes are undone by the "close without saving"
statement, so the original file is preserved)
Why does Excel rename the whole file when I ask to save a
worksheet????!? That is nonsense.
Any idea on how to achieve what I want?
Thanks.