How to Programatically Define Fieldinfo Parameter on Workbooks. Openttext in Excel Vba?

The Workbooks.OpenText method takes a parameter called FieldInfo to determine the format types in each column. For example the following code will open a text file and give Columns 1 and 2 a Text format, and Column 3 a General format.

Workbooks.OpenText fileName:="C:\somefile.txt", _
    FieldInfo:=Array(Array(1, 2), Array(2, 2), Array(3, 1))

Let's say at runtime I determine that my text file has 4 columns and columns 2 and 4 should be text. How can I pass that in programmatically?

2 Answers

According to the documentation:

FieldInfo (optional, Variant)

An array containing parse information for individual columns of data. The interpretation depends on the value of DataType. When the data is delimited, this argument is an array of two-element arrays, with each two-element array specifying the conversion options for a particular column.

  • the first element is the column number (1-based), and,
  • the second element is one of the XlColumnDataType constants specifying how the column is parsed.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.