importing a .csv so data on new lines?

B

biff

I have a csv file. The data is separated by commas and each record is on a
new line. Is there any way to import the data into Word so that the data
that is separated by commas in the csv appears on a new line within Word
while each record is separated by a blank line?
 
S

Steve Yandl

Here is one option.

'--------------------------------------------------------------


Sub SpecCSVimport()

Const ForReading = 1

Dim strPath As String
Dim strLine As String
Dim i As Integer
Dim arrFileLines() As String
Dim arrOneLine() As String
Dim fso

strPath = "C:\Test\My CSV.csv"

Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(strPath) Then
Exit Sub
End If

If Not LCase(fso.GetExtensionName(strPath)) = "csv" Then
Exit Sub
End If

Set objFile = fso_OpenTextFile(strPath, ForReading)
i = 0
Do Until objFile.AtEndOfStream
ReDim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop

objFile.Close

For n = 0 To UBound(arrFileLines)
strLine = ""
arrOneLine = Split(arrFileLines(n), ",")
For x = 0 To UBound(arrOneLine)
strLine = strLine & arrOneLine(x) & vbCrLf
Next x
Selection.TypeText Text:=strLine
Selection.TypeParagraph
Next n

Set fso = Nothing

End Sub

'--------------------------------------------------------------

Steve Yandl
 
Y

Yves Dhondt

Well you could just copy paste your data into Word and then do two find
replace operations:

1)Replace "^p" by "^p^p"
2)Replace "," by "^p"

If you don't want paragraphs but rather soft enters within a record, you
could use "^l" for the replacement option in the second part.

Yves
 
B

biff

Thank you both for the helpful suggestions.

Yves Dhondt said:
Well you could just copy paste your data into Word and then do two find
replace operations:

1)Replace "^p" by "^p^p"
2)Replace "," by "^p"

If you don't want paragraphs but rather soft enters within a record, you
could use "^l" for the replacement option in the second part.

Yves



.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top