How to import txt.file in EXCEL withVB 6


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to import txt.file in EXCEL withVB 6

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    How to import txt.file in EXCEL withVB 6

    [Originally posted by Ela]

    How to import txt.file in EXCEL with VB 6.0 with writen code?
    E

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:How to import txt.file in EXCEL withVB 6

    [Originally posted by Daniel Simpson-Dernovoy]

    You will need to create reference to "Microsoft Excel 8.0" object library in "references" in your VB 6 application.

    It really does depend on the format of your text file.˙ If it is a common CSV format, I have written the following function which takes a CSV text file and imports it to Excel.˙ Works VERY fast and gives you the option of viewing the worksheet after import (Providing you have Office installed on your PC)

    'Function: OpenExcelTextFile.˙ Opens comma delimited text file as an Excel worksheet(Opens worksheet to view)
    Function OpenExcelTextFile(ByVal CSVfpath As String, ByVal showworksheet As Boolean) As Integer
    ˙ ˙ On Error GoTo errtrap:
    ˙ ˙ Dim x As Excel.Application
    ˙ ˙ Dim xlconst As XlPlatform
    ˙ ˙ Dim xltype As XlTextParsingType
    ˙ ˙
    ˙ ˙ Set x = New Excel.Application
    ˙ ˙
    ˙ ˙ x.Workbooks.OpenText FileName:=CSVfpath, _
    ˙ ˙ DataType:=xlDelimited, comma:=True
    ˙ ˙
    ˙ ˙ If showworksheet = False Then
    ˙ ˙ ˙ ˙ x.ActiveWorkbook.Close True˙ ˙ 'save changes and close Excel instance
    ˙ ˙ Else
    ˙ ˙ ˙ ˙ x.ActiveWorkbook.SaveAs
    ˙ ˙ ˙ ˙ x.Visible = True
    ˙ ˙ End If

    Exit Function
    errtrap:
    ˙ ˙ MsgBox ("Error occured in [OpenExcelTextfile] - " & Err.Number & ": " & Err.Description)
    ˙ ˙ OpenExcelTextFile = -1 'error
    End Function




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center