Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Opening text file in Excel, fixed width, columns
Message
From
20/01/2003 06:23:48
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00743166
Message ID:
00743402
Views:
25
>Hi, I have just realized that Excel can open a text file, containing columns. And that it can place the columns ion the text file into Excel columns --- by using the fixed width options from File -- Open -- text file -- fixed widths.
>How would I then convert the following, taken from a recording the keystrokes in Excel -- into vfp excel automation ccode.
>
>
> Workbooks.OpenText Filename:="C:\d\tax\200210.txt", Origin:=xlWindows, _
> StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(14 _
> , 1), Array(42, 1), Array(67, 1), Array(70, 1), Array(83, 1))
>
>My guess that it would be something remotely like this but I can't figure out to get the Array stuff in there?
>
>o = CREATEOBJECT('excel.application')
>o.Application.Workbooks.Open(The_File, extra parameters, extra ?, extra? , ....... )
>
>
>any help would be greaqtly appreciated.
>Thanks
>Steve

Steve,
Array in excel is also array in VFP passed by ref.
OpenText is method name
xlWindows, xlFixedWidth etc are constants with numeric values.
Filename:="C:\d\tax\200210.txt" is a named parameter (that's "C:\d\tax\200210.txt" would go to parameter expecting 'Filename' - filename is the parameter name and "C:\d\tax\200210.txt" is value).
You can't use named parameters in VFP. Instead you need to know parameter position and directly put value to that position (positional parameter). Starting with Excel 2000 help correctly shows parameters with their positions. If you look Excel help for Opentext syntax is :

expression.OpenText(Filename, Origin, StartRow, DataType, TextQualifier, ConsecutiveDelimiter, Tab, Semicolon, Comma, Space, Other, OtherChar, FieldInfo, DecimalSeparator, ThousandsSeparator)

Further reading the help :
expression is a WorkBooks object
All parameters except first one (filename) are optional.
For fixed width data 2 dimension array defines start of column and its datatype (or skip column).

For your case import text is 6 columns where all is formatted with 'GeneralFormat'
#DEFINE xlDelimited	1	
#DEFINE xlFixedWidth	2	

#DEFINE xlGeneralFormat	1	
#DEFINE xlTextFormat	2	
#DEFINE xlMDYFormat	3	
#DEFINE xlDMYFormat	4	
#DEFINE xlYMDFormat	5	
#DEFINE xlMYDFormat	6	
#DEFINE xlDYMFormat	7	
#DEFINE xlYDMFormat	8	
#DEFINE xlSkipColumn	9	
#DEFINE xlEMDFormat	10	

lcFileName = "C:\d\tax\200210.txt"
dimension arrFldInfo[6,2]
arrFldInfo = 1 && All Generalformat
arrFldInfo[1,1]=0
arrFldInfo[2,1]=14
arrFldInfo[3,1]=42
arrFldInfo[4,1]=67
arrFldInfo[5,1]=70
arrFldInfo[6,1]=83

oExcel = createobject('Excel.Application')
with oExcel
 .Workbooks.OpenText(lcFilename,,,xlFixedWidth,,,,,,,,,@arrFldInfo)
 .visible = .t.
endwith
PS:In VFP7 you can get the constants written for you with object browser drag&drop.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform