|
-
May 31st, 2006, 06:07 PM
#1
[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:
Option Explicit
'Behind ThisWorkbook
Public Sub SaveAsXLS()
Application.ActiveWorkbook.SaveAs Filename:="C:\Book1.xls", FileFormat:=xlWorkbookNormal
Application.ActiveWorkbook.Saved = True
End Sub
'Usage:
'Call the sub from behind any worksheet or you can link it to a toolbar button etc.
Private Sub Command0_Click()
Call SaveAsXLS
End Sub
VB 6 Code Example:
VB Code:
Option Explicit
'Add a reference to MS Excel xx.0 Object Library
Private Sub Command1_Click()
Dim oApp As Excel.Application
Dim oWB As Excel.Workbook
'Create an Excel instalce and open the csv file.
Set oApp = New Excel.Application
'If its a comman separated values file then the Comma argument will be true
'If its Tab delimited we will use the Tab argument and set it to True, and so forth.
oApp.Workbooks.OpenText FileName:="C:\test.csv", Origin:=xlMSDOS, DataType:=xlDelimited, Comma:=True
'Set a workbook variable to the opened csv file
Set oWB = oApp.Workbooks("test.csv")
'Save the csv as a xls file
oWB.SaveAs FileName:="C:\test.xls", FileFormat:=xlWorkbookNormal
'Tag the workbook as saved to surpress the "Do you want to save" prompt
oWB.Saved = True
'close and clean up resources
oWB.Close
Set oWB = Nothing
oApp.Quit
Set oApp = Nothing
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 28th, 2008, 01:51 PM
#2
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:
Option Explicit On
Option Strict On
'Add a COM reference to MS Office Excel xx.0 Object Library
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oApp As Excel.Application
Try
'Create an Excel instalce.
oApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
'Open the csv file
oApp.Workbooks.OpenText(Filename:="C:\Test.csv", _
Origin:=Excel.XlPlatform.xlMSDOS, _
DataType:=Excel.XlTextParsingType.xlDelimited, _
Comma:=True, _
StartRow:=1)
'Optional
oApp.Visible = True
'Now lets SaveAs to convert it to an xls file format.
oApp.Workbooks.Item("Test.csv").SaveAs(Filename:="C:\Test.xls", _
FileFormat:=Excel.XlFileFormat.xlWorkbookNormal)
'Now lets close the workbook. No need to save the changes as we just did a saveas
oApp.Workbooks.Item("Test.xls").Close(SaveChanges:=False)
'Quit Excel
oApp.Quit()
oApp = Nothing
MessageBox.Show("Done", "Done", MessageBoxButtons.OK)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK)
End Try
End Sub
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|