Hi Rob,
> No luck as yet. Do you think it would be best to copy the txt to a new
> workbook and save as normal?
You shoudn't have to, so let's save that option for later.
I am not really sure where you run into problems at this point. Is it
the SaveAs() call itself with its arguments? An invalid numerical value
for the second argument will crash your application. Is that what happens?
I had a chance to test today what I wrote yesterday, and the test worked
like I had expected. I created a worksheet from scratch just like in the
samples that can be found in many previous workgroup discussions. It was
just a small sample, so I did no import. But I could save the workbook
as both .csv and .xls with no problems by changing the second argument.
Ron de Bruin lists all the relevant FileFormatNum numbers in this page:
http://www.rondebruin.nl/win/s5/win001.htm
I used 6 for .csv and -4143 for .xls because I was using an old Excel
version that did not understand FileFormatNum number 56, and -4143 is
the xlWorkbookNormal constant, or the default.
Excel's ability to understand your specific FileFormatNum number is one
thing to be careful about. As you can see in Ron de Bruin's page that I
linked to above, older Excel versions do not understand new
FileFormatNum numbers, just like they do not understand the
corresponding files. And 56 only became the .xls format number in Excel
2007. So to make sure your application will not blow up, leaving Excel
dangling, first check the application version number, and then set the
FileFormatNum number to something that particular Excel version
understands. The Excel Application version numbers can also be found in
that web page.
Assuming that <oExcel> is the name of your OLE object, then you can get
the Excel version with:
nExcelVersion := val( oExcel:Version )
Then you check (at least) if <nExcelVersion> is less than 12 or not, and
choose a valid FileFormatNum number for the resulting file type you
want. I would suggest to start with -4143 and take it from there. Then
you save your workbook using oWorkBook:SaveAs() passing the filename and
the number - just the number, no argument name.
Does that work? If not, can you post a complete and reduced sample of
the code that does not work properly, leaving out everything that is not
absolutely necessary?
Regards,
Klas