Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to a CSV file

Status
Not open for further replies.

perltk07

Programmer
Apr 2, 2007
13
US
I want my VB program to create a csv file and then i want to write to my csv file and have the fields written in particular columns. This is what I have so far

Dim oCSV As Object
Dim oBook As Object
Dim oSheet As Object

'Start a new workbook in CSV.
oCSV = CreateObject("CSV.Application")
oBook = oCSV.Workbooks.Add

'The inputs are written to the CSV file
oSheet = oBook.Worksheets(1)
oSheet.Range("D4").Value = "ProjectTextBox.Text"
oSheet.Range("B1").Value = "NotesTextBox.Text"
'oSheet.Range("A2").Value = "Doe"
'oSheet.Range("B2").Value = "John"
'Save the Workbook and quit Excel.
oCSV.ActiveWorkbook.SaveAs("C:\Program Files\CSV\doc1")

oSheet = Nothing
oBook = Nothing
oCSV.Quit()
oCSV = Nothing
GC.Collect()

Can someone help me please
 

From your code, it comes to know that you are using Excel
Referenced Code said:
'Save the Workbook and quit Excel.
oCSV.ActiveWorkbook.SaveAs("C:\Program Files\CSV\doc1")

If this is true you can change your code to the following to get your CSV file.

Code:
oCSV.ActiveWorkbook.SaveAs[b](Filename:="C:\Program Files\CSV\doc1.csv", FileFormat:=xlCSV, CreateBackup:=False)[/b]

Tell me if you are not using Excel and you have been developing your own application like excel and you want to implement the functionality of saving data in CSV format from your application.

Hope this helps.

 
I have done this very thing by building a comma separated string and then written this to a file with a .csv extension.

This I think would be much quicker than what you are trying to do. If Excel is installed on the target machine it will automatically open with Excel.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top