Filling Table with csv file

C

Clint

I'm trying to create a transmittal letter with a list of drawings. The list
of drawing information is in a csv file and I want to create a template that
will take this csv file and create a list of the drawing information. In
other words all the information from the csv file listed on one letter.
Help!!! I tried to use mail merge but all I'm getting is the first record.
 
J

Jay Freedman

Clint said:
I'm trying to create a transmittal letter with a list of drawings.
The list of drawing information is in a csv file and I want to create
a template that will take this csv file and create a list of the
drawing information. In other words all the information from the csv
file listed on one letter. Help!!! I tried to use mail merge but all
I'm getting is the first record.

If you have Excel, open the csv file in that program -- Excel understands
csv, while Word considers it just plain text. Then select the cells in
Excel, copy to the clipboard, and paste into Word. The result will be a
table.

Without Excel, you can open or paste the csv data into Word, select it, and
run Table > Convert > Text to Table. Make sure the "Commas" option is
selected for the separator and click OK. The result will be a table similar
to the one you'd get from Excel, but formatted somewhat differently.
 
C

Clint

I want this done as a template. So when a user opens the template it will
update the list of drawings for each transmittal letter. The csv file is
always named the same....
 
J

Jay Freedman

OK, that's doable. The UpdateTableFromCSV() macro below automates the
"convert to table" method that I mentioned before.

Put these three macros in a module in your template (see
http://www.gmayor.com/installing_macro.htm if you need instructions). Change
the path and name of the csv file (in the line that defines the variable
csvFileName) to the one you use.

Public Sub AutoNew()
UpdateTableFromCSV
Dialogs(wdDialogFileSaveAs).Show
End Sub

Public Sub AutoOpen()
UpdateTableFromCSV
ActiveDocument.Save
End Sub

Private Sub UpdateTableFromCSV()
Dim oRg As Range
Dim oDoccsv As Document
Dim csvFileName As String

csvFileName = _
"C:\Documents and Settings\Clint\My Documents\Data.csv"

Set oRg = ActiveDocument.Bookmarks("csv_here").Range

On Error Resume Next
Set oDoccsv = Documents.Open( _
FileName:=csvFileName, _
ConfirmConversions:=False, _
Format:=wdOpenFormatText)
If Not oDoccsv Is Nothing Then
With oRg
Do While .Tables.Count
.Tables(1).Delete
Loop

.Text = oDoccsv.Content.Text

oDoccsv.Close savechanges:=wdDoNotSaveChanges
Set oDoccsv = Nothing

.Style = ActiveDocument.Styles("Normal")
.ConvertToTable Separator:=","
End With
ActiveDocument.Bookmarks.Add Name:="csv_here", _
Range:=oRg.Tables(1).Range
Else
MsgBox "Could not find " & csvFileName
End If
End Sub

Add a bookmark named "csv_here" in the body of the template and save it.
Then use File > New to create a document based on the template. Each time
you create a new document or open an existing document based on this
template, the table will be updated to the current contents of the csv file.
 

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