Results 1 to 2 of 2

Thread: [FAQ's: OD] How do I convert a delimited file to an xls FileFormat?

Threaded View

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    [FAQ's: OD] How do I convert a delimited file to an xls FileFormat?

    To convert a csv or other delimited file you only need to open it in Excel and perform a .SaveAs and specify the format you wish it to be saved as and where.

    To open the file we will use the .OpenText method. This method allows for the opening of several types of files.

    Once opened we will .SaveAs and specify the location and fileformat desired.


    Excel VBA Code Example:

    VB Code:
    1. Option Explicit
    2. 'Behind ThisWorkbook
    3. Public Sub SaveAsXLS()
    4.     Application.ActiveWorkbook.SaveAs Filename:="C:\Book1.xls", FileFormat:=xlWorkbookNormal
    5.     Application.ActiveWorkbook.Saved = True
    6. End Sub
    7.  
    8. 'Usage:
    9. 'Call the sub from behind any worksheet or you can link it to a toolbar button etc.
    10. Private Sub Command0_Click()
    11.     Call SaveAsXLS
    12. End Sub

    VB 6 Code Example:
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Excel xx.0 Object Library
    3. Private Sub Command1_Click()
    4.     Dim oApp As Excel.Application
    5.     Dim oWB As Excel.Workbook
    6.    
    7.     'Create an Excel instalce and open the csv file.
    8.     Set oApp = New Excel.Application
    9.     'If its a comman separated values file then the Comma argument will be true
    10.     'If its Tab delimited we will use the Tab argument and set it to True, and so forth.
    11.     oApp.Workbooks.OpenText FileName:="C:\test.csv", Origin:=xlMSDOS, DataType:=xlDelimited, Comma:=True
    12.     'Set a workbook variable to the opened csv file
    13.     Set oWB = oApp.Workbooks("test.csv")
    14.     'Save the csv as a xls file
    15.     oWB.SaveAs FileName:="C:\test.xls", FileFormat:=xlWorkbookNormal
    16.     'Tag the workbook as saved to surpress the "Do you want to save" prompt
    17.     oWB.Saved = True
    18.     'close and clean up resources
    19.     oWB.Close
    20.     Set oWB = Nothing
    21.     oApp.Quit
    22.     Set oApp = Nothing
    23. End Sub
    Last edited by RobDog888; Jan 14th, 2007 at 06:28 PM. Reason: Mis-spelling; fixed oWB issue (thanks Bushmobile ;))
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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