Cannot open text workbook

J

jat

i am using the following code to open two txt files that open fine in notepad:

Workbooks.OpenText "C:\Orders\" & Range("B2"), Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, _
1), Array(10, 1), Array(40, 1), Array(58, 1), Array(68, 1)),
TrailingMinusNumbers:=True

this first code works fine to open the file and edit the document in excel
2007.

the following code in a second macro gives an error message:
"This file is not in a recognizable format...., if you know the file is from
another program which is incompatible...., if you suspect the file is
damaged,......, if you still want to see what text is contained .....

the code to open the second file is:
Workbooks.OpenText Filename:="C:\Orders\" & Range("B4"), Origin:= _
xlWindows, StartRow:=1, DataType:=xlFixedWidth,
FieldInfo:=Array(Array(0, _
1), Array(10, 1), Array(18, 1), Array(30, 1), Array(60, 1),
Array(66, 1)), TrailingMinusNumbers:=True

what's wrong with the second code?

jat
 
O

OssieMac

Hi Jat,

Can you open the file manually in Excel? If not, what type of file extension
does it have; is it .txt? If you cannot see the file extension then open
Control Panel -> Folder Options -> View tab and uncheck 'Hide extensions for
known file types'.

If it is not .txt then copy the file to a new filename and then rename it to
..txt (Ignore the warnings about renaming) and try to open the new file with
Excel again and if that works test the code again with the renamed file.

If code is still not working, record a macro to open it and compare the
recorded code with the code you posted. If you still can't sort it out then
post the recorded code so that I (or someone else) can compare it.
 

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