Results 1 to 3 of 3

Thread: Open Excel From VB (to display a text file)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Posts
    3

    Open Excel From VB (to display a text file)

    I have a tab-delimited text-file which I want to allow user to view in Excel. How can i Do that?
    I had a few thoughts:

    1) rename the text file to .xls(i used ms-dos). Then I could open the file from VB using GetObject(). Excel opens fine. But problem is dunno how to copy or rename files from VB (instead of going manually to ms-dos)

    2) open excel, wkbk, wksheets and populate data one-by-one using recordset. But i realise that that when using recordset, we need to know the flds involve....my text file is a matrix (rows and columns) data file...and i have no knowledge of the size...thus can't know how many flds involve.

    Any help is appreciated.

    3) or is there a way to open text file in excel (control from VB of cos) automatically??

    Thanks in advance....

  2. #2
    Junior Member
    Join Date
    Dec 2003
    Posts
    16
    you can try this
    (with including Microsoft Excel x.x Object Library)

    VB Code:
    1. Sub MakeXL(csvPath As String, NewXLPath As String, NewXLSheetName As String, Delimiter As String)
    2.  
    3. Dim wb As Workbook, ws As Worksheet
    4. Set wb = Workbooks.Add: Set ws = wb.Worksheets.Add
    5. ws.Name = NewXLSheetName
    6.  
    7. ff = FreeFile
    8. Open csvPath For Input As ff
    9. On Error Resume Next
    10.     Do While Not EOF(ff)
    11.     Line Input #ff, buff
    12.     n = n + 1
    13.     buff = Split(buff, Delimiter)
    14.         If IsArray(buff) Then
    15.             For c = 0 To UBound(buff)
    16.               ws.Range(Chr(c + 65) & n) = buff(c)
    17.               If Err Then
    18.                 'if the first char of the item is -,+,= or etc. an error occures
    19.                 ws.Range(Chr(c + 65) & n) = "'" & buff(c)
    20.                 'so we add a ' sign to the first char
    21.               End If
    22.             Next c
    23.         Else
    24.             ws.Range(Chr("A" & n)) = buff
    25.         End If
    26.         If Err Then MsgBox "Error on col:" & c + 1 & " row:" & n & vbCrLf & Err.Description
    27.         Err.Number = 0
    28.     Loop
    29. Close #ff
    30.  
    31. wb.Close True, NewXLPath
    32. End Sub

    i tried the code and it ok

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Posts
    3

    Thumbs up

    Thanks a mill....it works!!!!

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