Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening .txt file as Excel Workbook

Status
Not open for further replies.

REDYPS

Programmer
Jun 12, 2003
12
US
I need to create an extract process for an application. The end user insists that the file be displayed in Excel.

I wrote code to create a .txt file easily enough. However I get the following error when I try to open the file.

An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayTypeMismatchException' occurred in mscorlib.dll

Additional information: Specified array was not of the expected type.

Here is the code:

Dim oXL As Object
Dim intLen As Integer
Dim strTemp As String
Dim varTempArray() As VariantType = {0, 0}
Dim varJaggedArray(9)() As VariantType ' array of arrays

Dim i As Integer
For i = 0 To 9
varTempArray(0) = i + 1
varTempArray(1) = 2
varJaggedArray(i) = varTempArray
Next


' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oXL.Visible = True

'open workbook
oXL.Workbooks.OpenText(strF, StartRow:=1, _
DataType:=Excel.XlTextParsingType.xlDelimited, _
TextQualifier:=Excel.XlTextQualifier.xlTextQualifierSingleQuote, TAB:=True, FieldInfo:=varJaggedArray)

If I remove FieldInfo:=varJaggedArray from the OpenText statement the code works fine and opens the document. The problem is that I need the columns formatted as text. Some of the data is numeric in nature with leading zeroes and I need to maintain the leading zeroes.

Any suggestions?? Thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top