Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workbooks.OpenText doesn't take 'fieldinfo' parameter

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.

  • The two-dimensional array: first number is the column number, second number stands for data type where 2 stands for text
  • I have to use the 'local'-parameter because I'm on a localized OS with a localized Office 2003
  • The operating system is irrelevant since it happens on Windows XP and on Windows 7

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.

like image 838
nixda Avatar asked Mar 04 '12 22:03

nixda


1 Answers

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

like image 157
nixda Avatar answered Oct 11 '22 16:10

nixda