View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Newbie Scriptor[_2_] Newbie Scriptor[_2_] is offline
external usenet poster
 
Posts: 1
Default how to lmport a text file and save it as an excel file using v

Lonnie,
Thanks for your reply. I guess your code works only after opening up an
excel instance and importing and selecting the text into a column. However
what I am trying to do is in a batch mode with no manual interaction, create
a new excel file, read into it a text file (which is tab delimited , so
opentext method should automatically be able to handle the parsing into
multiple columns) and then save it as a normal formatted Excel file. My
sample code does correctly upto formatting the imported text. However whne
trying to save it as an Excel file it brings up the "SaveAs" dialog box. I am
running the script using "cscript.exe" not as a macro from within Excel
workbook.

Thanks and hope you can help me further
Newbie scriptor

"Lonnie M." wrote:

I'm a Newbie too,
but I think you need to run text-to-columns after you open the file.
When import a text file it all gets dumped into the first column.
Then you can format, autofit, and save.

See the examples below (Text-to-columns):
Workbooks.Open FileName:=Fname

'Method I
Set mySheet = ActiveSheet
myShtName = ActiveSheet.Name
R = mySheet.UsedRange.Rows.Count
mySheet.Range(Cells(1, 1), Cells(R, 1)).Select
Application.Dialogs(xlDialogTextToColumns).Show
Columns.AutoFit

'Method II
Application.Dialogs(xlDialogOpen).Show Arg1:=Fname
Set mySheet = ActiveSheet
myShtName = ActiveSheet.Name
R = mySheet.UsedRange.Rows.Count
Columns.AutoFit

See the examples below (Saving file as 'xls'):
In VBA the code for saving the file would look like this, where Fname
is a string; I.E: Fname = ThisWorkbook.Path & Application.PathSeparator
& "MyFile.xls":
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=Fname, FileFormat:=xlNormal
Application.DisplayAlerts = True

HTH, Lonnie