Workbooks.OpenText doesn't take 'fieldinfo' parameter

nixda picture nixda · Mar 4, 2012 · Viewed 8.5k times · Source

The following code demonstrates the problem which is stated in questions title.

Copy & paste it in a new Microsoft Excel 2003 workbook.

Sub mytest()
    mypath = Application.GetSaveAsFilename()

    Workbooks.OpenText Filename:=mypath, DataType:=xlDelimited, _ 
        TextQualifier:=xlTextQualifierDoubleQuote, _ 
        semicolon:=True, _ 
        fieldinfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2)), _ 
        Local:=True
End Sub

Execute the code. It will ask for an input file where you should use this semicolon-seperated test.csv. It creates a new workbook and imports all the data from test.csv to sheet1.

The picture below shows the result
enter image description here

But It should have shown a result like this
enter image description here


fieldinfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2))
states that Excel should treat all imported data as text. Unfortunately it doesn't.

Can someone show me how to use opentext and fieldinfo in the correct way?

I already know the work-around with QueryTables.Add(Connection[...].
But thats not a solution for my case.

Answer

nixda picture nixda · Mar 5, 2012

If you rename the extension from the input file from .csv to .txt the fieldinfo parameter works as designed.