Results 1 to 2 of 2

Thread: adding extra info to an excel file after it has read a txt file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2011
    Posts
    255

    adding extra info to an excel file after it has read a txt file

    hi

    i have this code that reads a txt file of mind

    Code:
    Dim oExcel As Object  
            Dim oBook As Object  
            Dim oRow As Int16 = 0  
      
            oExcel = CreateObject("Excel.Application")  
            oBook = oExcel.Workbooks.Add  
      
            'Read input .txt file line-by-line, Copy to Clipboard & Paste to Excel  
      
            Using rdr As New System.IO.StreamReader("C:\Users\rr154237\Desktop\AccInfo.txt")  
                Do While rdr.Peek() >= 0  
                    Dim InputLine As String = rdr.ReadLine  
                    oRow = oRow + 1  
                    System.Windows.Forms.Clipboard.SetDataObject(InputLine)  
                    oBook.Worksheets(1).Range("A" + oRow.ToString).Select()  
                    oBook.Worksheets(1).Paste()  
                Loop  
                rdr.Close()  
            End Using  
      
            oExcel.Visible = True  
            oExcel.save("C:\Users\rr154237\Desktop\test.xls")  
            oBook = Nothing  
            oExcel.Quit()  
            oExcel = Nothing
    works fine it does what i want it read the two coulmns from txt file and puts it into excel sheet.



    what i need to do with this code is a dd a column header to column c say value is the header and i want to add in number to the excel file.

    for example lets say i want all values to be set to 0.00

    it wont be in txt file ever so how can i get it to add to excel sheet from the code i have done so far

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: adding extra info to an excel file after it has read a txt file

    You could add something like this:

    Code:
    oBook.worksheets(1).range("c1").value = "Header"    'change to whatever sheet required
            oExcel.save()
    after setting "oBook" to your newly named Excel file:

    Code:
    oBook = oExcel.workbooks.open("C:\Users\rr154237\Desktop\test.xls")
    (make sure it's not open first)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width