Home |
Search |
Today's Posts |
|
#1
|
|||
|
|||
Filling Table with csv file
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. |
#2
|
|||
|
|||
Clint wrote:
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. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org |
#3
|
|||
|
|||
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.... "Jay Freedman" wrote: Clint wrote: 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. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org |
#4
|
|||
|
|||
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. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Clint wrote: 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.... "Jay Freedman" wrote: Clint wrote: 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. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org |
#5
|
|||
|
|||
Thanks, this really helped
"Jay Freedman" wrote: 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. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Clint wrote: 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.... "Jay Freedman" wrote: Clint wrote: 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. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
How to creat relative and shorthand file path names? | Microsoft Word Help | |||
How can I insert a text file into a table? | Tables | |||
Table headers/footers and layout | Page Layout | |||
Table AutoFormats vs. Table Styles confusion | Tables | |||
how do i determine end of table in MS word file? | Tables |