VBA/Excel - Mailmerge from a .csv file

stemby

New Member
Joined
Mar 13, 2002
Messages
26
Is there any way, using VBA in Excel, I can take the contents of a .CSV file and merge it with a word doc?

I have a .csv file called 'mailmerge.csv' saved in c:\temp.

Borrower NumberFirst_NameLast_NameDate_of_BirthGenderInput_BranchSchool_NameNew_MemberBooks_ReadMini_Challenge
B66666666BarryTrotter
12/12/2018​
BoyGRSchool XY
6​
Y
B12121213WalterWilbursmith
12/12/2018​
BoyGRSchool YY
6​
Y

The number of rows in this .csv will vary, but these are the columns.

I'm looking to place a button on a form in one of my spreadsheets that will open word, then pull out all the first_names and last_names from this .CSV, and then merge them into a word doc for printing certificates.

Is there a bit of VBA code I can use (or can someone point me in the right direction?). The certificates are all 'pre-printed', so basically I just need the first_name/last_name outputting to a certain location on a page, e.g. centered in the middle (although this may vary now and again), then onto the next first_name/Last_name on the second page, and so on...

I'm not a programmer and new to VBA, so please be gentle with me :)

Many thanks,
Mark
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I've found some code that seems to do the trick:

VBA Code:
Sub RunMerge()
    Dim strWorkbookName As String
    strWorkbookName = "C:\Temp\Mailmerge.csv"
    Dim wdapp As New Word.Application
    Dim wddoc As Word.Document
    With wdapp
        
        'Disable alerts to prevent an SQL prompt
        .DisplayAlerts = wdAlertsNone
        
        'Open the mailmerge main document
        Set wddoc = .Documents.Open(ThisWorkbook.Path & "\Certificates-Mailmerge.docx")
        With wddoc
            With .MailMerge
                
                'Define the mailmerge type
                .MainDocumentType = wdFormLetters
                
                'Connect to the data source
                .OpenDataSource Name:="C:\Temp\Mailmerge.csv", AddToRecentFiles:=False, _
                Revert:=False, Format:=WdOpenFormat.wdOpenFormatAuto, Connection:="Data Source=" _
                & strWorkbookName & ";Mode=Read", SQLStatement:="SELECT * FROM 'Mailmerge'"
                .SuppressBlankLines = True
                With .DataSource
                    .FirstRecord = wdDefaultFirstRecord
                    .LastRecord = wdDefaultLastRecord
                End With
                
                'Define the output
                .Destination = wdSendToNewDocument
                
                'Excecute the merge
                .Execute
                
                'Disconnect from the data source
                .MainDocumentType = wdNotAMergeDocument
            End With
            
            'Close the mailmerge main document
            .Close False
        End With
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top