problem with textfiles to Excel

cesarvb6

New member
Joined
Mar 19, 2011
Messages
1
Programming Experience
Beginner
Hi: This program only write in excel the second sheet and nothing in the first sheet, i dont know why, thanks for your help, the program reads to text files



Dim xl As Object
xl = CreateObject("Excel.Application")
xl.Visible = True
xl.Workbooks.Add()
xl.Sheets("Sheet" & 1).Select()
xl.Workbooks.OpenText(Filename:="C:\Documents and Settings\User\Desktop\429\sheet1" & ".txt", Origin:=Excel.XlPlatform.xlMSDOS, StartRow:=1, _
DataType:=Excel.XlTextParsingType.xlFixedWidth, TextQualifier:=Excel.XlTextQualifier.xlTextQualifierNone, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False, _
FieldInfo:=New Object() {New Object() {0, 2}, New Object() {9, 9}, _
New Object() {11, 1}, New Object() {12, 9}, New Object() {14, 1}, New Object() {46, 1}, _
New Object() {56, 9}, New Object() {58, 5}, New Object() {64, 9}, New Object() {66, 1}, _
New Object() {74, 9}, New Object() {76, 1}, New Object() {82, 9}, New Object() {84, 1}, _
New Object() {89, 9}, New Object() {91, 5}, New Object() {97, 9}, New Object() {99, 5}, _
New Object() {105, 9}, New Object() {107, 5}, New Object() {113, 9}, New Object() {115, 1}, _
New Object() {116, 9}})
xl.Sheets("Sheet" & 1).name = "cesar"


xl.Workbooks.Add()
xl.Sheets("Sheet" & 2).Select()
xl.Workbooks.OpenText(Filename:="C:\Documents and Settings\User\Desktop\429\sheet2" & ".txt", Origin:=Excel.XlPlatform.xlMSDOS, StartRow:=1, _
DataType:=Excel.XlTextParsingType.xlFixedWidth, TextQualifier:=Excel.XlTextQualifier.xlTextQualifierNone, _
ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False, _
FieldInfo:=New Object() {New Object() {0, 2}, New Object() {9, 9}, _
New Object() {11, 1}, New Object() {12, 9}, New Object() {14, 1}, New Object() {46, 1}, _
New Object() {56, 9}, New Object() {58, 5}, New Object() {64, 9}, New Object() {66, 1}, _
New Object() {74, 9}, New Object() {76, 1}, New Object() {82, 9}, New Object() {84, 1}, _
New Object() {89, 9}, New Object() {91, 5}, New Object() {97, 9}, New Object() {99, 5}, _
New Object() {105, 9}, New Object() {107, 5}, New Object() {113, 9}, New Object() {115, 1}, _
New Object() {116, 9}})

xl.Sheets("Sheet" & 2).name = "martinez"
xl.Worksheets(1).SaveAs("C:\Documents and Settings\User\Desktop\filetemp\bo" & ".xls", Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange)


xl.Quit()
xl = Nothing
 
Trying using the following objects when working with sheets and book.

VB.NET:
Dim xl as New Excel.Application
Dim xlBook as Excel.Workbook = xl.Workbooks.Add()
Dim xlSheet1 as Excel.Worksheet = xlBook.Sheets("Sheet1")
Dim xlSheet2 as Excel.Worksheet = xlBook.Sheets("Sheet2")
Dim xlSheet3 as Excel.Worksheet = xlBook.Sheets("Sheet3")

When a new book is created is starts with 3 sheets 1,2, and 3 by default. You may be confused by the excel structure Application --> Workbooks --> Sheets an application can open multiple workbooks and a workbook contains one or more sheets.
 
Back
Top