# [SOLVED] Mail Merge from CSV via macro



## rodloc (Oct 4, 2009)

Hi,

I have the following macro script that was being used with an excel file to mail merge documents and spit out separate pdfs for each row.

However i have had to move from xlsx file to a csv file as I have more than 255 columns that i am trying to mail merge (I cannot make it smaller)

What I want to do use the csv in the same process however the macro is not able to run now.

I am using Word 2010 as a .doc file because of images within my mail merge.

Hopefully someone can help

Thanks,

Rod

Macro script is underneath

Sub MacroMergetoPDF()
'
' MacroMergetoPDF Macro
'
'
MainDoc = ActiveDocument.Name
For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i
docName = .DataFields("SH").Value & " " & .DataFields("BSN").Value & " " & .DataFields("Structure_Name").Value & ".pdf"
End With
.Execute Pause:=False
End With
Selection.WholeStory
Selection.Fields.Update

ActiveDocument.ExportAsFixedFormat OutputFileName:=docName, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ChangeFileOpenDirectory "P:\393\3933318\TGI\55_Workspaces\02_wor\PDF\"
ActiveWindow.Close SaveChanges:=False

Documents(MainDoc).Activate
Next i
Application.ScreenUpdating = True
End Sub


----------



## macropod (Apr 11, 2008)

*Re: Mail Merge from CSV via macro*

A mailmerge with Excel can handle more than 255 columns ...


----------



## rodloc (Oct 4, 2009)

*Re: Mail Merge from CSV via macro*

Hi Paul,

I have tried it in excel however not all the fields come through. I have checked in excel at it stops at column 255. So is there some settings in the excel or word that I can use to allow mor fields.

Thanks,

Rod


----------



## macropod (Apr 11, 2008)

*Re: Mail Merge from CSV via macro*

Although you can't access more that 255 fields via the dialogs, you can add them manually if you know what the names are. To do that, position the insertion point at the destination, press Ctrl-F9 to create a pair of field braces (i.e. { }) and type 'MERGEFIELD MyField' between them, where 'MyField' is the field name, so you end up with {MERGEFIELD MyField}. Press F9 to update.


----------



## rodloc (Oct 4, 2009)

*Re: Mail Merge from CSV via macro*

Hi Paul,

So I should be able to add the fields via a CSV then change it back to an excel

Thanks,

Rod


----------



## macropod (Apr 11, 2008)

*Re: Mail Merge from CSV via macro*

Huh? You have to add the fields to the Word document...


----------



## rodloc (Oct 4, 2009)

*Re: Mail Merge from CSV via macro*

So I have to add the extra fields into the word document manually.

So there is no way to get the macro to run with a CSV file. This is just because there are quite a number of fields that i would have to add manually. If I go the excel route.

Thanks,

Rod


----------



## macropod (Apr 11, 2008)

*Re: Mail Merge from CSV via macro*

I can't see what adding the fields to the document has to do with your macro.

You add the fields to the document manually, just as you've done for whichever of the other 255 fields you're using; the only difference is that you can't add them through the Insert Mergefield dialogue and need to use a more 'hands-on' approach.

You can mess around all you like with the data, but that won't get the fields into the _document_. Regardless of whether you use the CSV or an Excel file, you still have to add the fields to the _document_.


----------

