Results 1 to 2 of 2

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

  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

  2. #2

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

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

    Visual Basic .NET 2003-2008 And Excel 2000-2003
    VB.NET Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Add a COM reference to MS Office Excel xx.0 Object Library
    4. Imports Microsoft.Office.Interop
    5.  
    6. Public Class Form1
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         Dim oApp As Excel.Application
    10.         Try
    11.             'Create an Excel instalce.
    12.             oApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    13.             'Open the csv file
    14.             oApp.Workbooks.OpenText(Filename:="C:\Test.csv", _
    15.                                     Origin:=Excel.XlPlatform.xlMSDOS, _
    16.                                     DataType:=Excel.XlTextParsingType.xlDelimited, _
    17.                                     Comma:=True, _
    18.                                     StartRow:=1)
    19.             'Optional
    20.             oApp.Visible = True
    21.             'Now lets SaveAs to convert it to an xls file format.
    22.             oApp.Workbooks.Item("Test.csv").SaveAs(Filename:="C:\Test.xls", _
    23.                                                    FileFormat:=Excel.XlFileFormat.xlWorkbookNormal)
    24.             'Now lets close the workbook. No need to save the changes as we just did a saveas
    25.             oApp.Workbooks.Item("Test.xls").Close(SaveChanges:=False)
    26.             'Quit Excel
    27.             oApp.Quit()
    28.             oApp = Nothing
    29.             MessageBox.Show("Done", "Done", MessageBoxButtons.OK)
    30.         Catch ex As Exception
    31.             MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK)
    32.         End Try
    33.     End Sub
    34.  
    35. End Class
    Last edited by RobDog888; Jun 28th, 2008 at 01:56 PM.
    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