Results 1 to 5 of 5

Mailmerge macro changes needed

Threaded View

  1. #1
    Forum Contributor aganesan99's Avatar
    Join Date
    04-26-2013
    Location
    Chennai, India
    MS-Off Ver
    Excel 2010
    Posts
    129

    Mailmerge macro changes needed

    Hi,

    I am using the following code to perform mailmerge and save as individual doc in a separate folder.

    Option Explicit
    Sub Mailmerge_Create_Letters()
    ' Note: you will need to add error-trapping
    Dim objX As Object
    Dim rng1 As Range
    Dim rng2 As Range
    Dim wb As Workbook
    Dim wsControl As Worksheet
    Dim wsData As Worksheet
    '
    Dim oApp As Word.Application
    Dim oBookMark As Word.Bookmark
    Dim oDoc As Word.Document
    '
    Dim strDocumentFolder As String
    Dim strTemplate As String
    Dim strTemplateFolder As String
    Dim lngTemplateNameColumn As Long
    Dim strWordDocumentName As String
    Dim lngDocumentNameColumn As Long
    Dim lngRecordKount As Long ' not used but retained for future use
    '
    Set wb = ThisWorkbook
    Set wsControl = wb.Worksheets("Control Sheet")
    wsControl.Activate
    Set wsData = wb.Worksheets(wsControl.[Data_Sheet].Value)
    strTemplateFolder = wsControl.[Template_Folder].Value
    strDocumentFolder = wsControl.[Document_Folder].Value
    wsData.Activate
    lngTemplateNameColumn = wsData.[Template_Name].Column
    lngDocumentNameColumn = wsData.[Document_Name].Column
    ' number of letters required:
    ' must not have any blank cells in column A - except at the end
    Dim a As String
    a = InputBox("Please enter the begining row", "Begining row value")
    Set rng1 = wsData.Range(Cells(a, 1), Cells(Rows.Count, 1).End(xlUp))
    lngRecordKount = rng1.Rows.Count
    '
    'Set oApp = CreateObject("Word Application")
    Set oApp = New Word.Application
    ' Process each record in turn
    For Each rng2 In rng1
        strTemplate = strTemplateFolder & "\" & wsData.Cells(rng2.Row, lngTemplateNameColumn)
        strWordDocumentName = strDocumentFolder & "\" & wsData.Cells(rng2.Row, lngDocumentNameColumn)
        ' check that template exists
        If Dir(strTemplate) = "" Then
            MsgBox strTemplate & " not found"
            GoTo Tidy_Exit
        End If
        Set oDoc = oApp.Documents.Add
        oApp.Selection.InsertFile strTemplate
        ' locate each bookmark
        For Each oBookMark In oDoc.Bookmarks
            Set objX = wsData.Rows(1).Find(oBookMark.Name, LookIn:=xlValues, LookAt:=xlWhole)
            If Not objX Is Nothing Then
                ' found
                If Right(oBookMark.Name, 4) = "Date" Then
                    oBookMark.Range.Text = Format(wsData.Cells(rng2.Row, objX.Column), "dd mmmm yyyy")
                'ElseIf Right(oBookMark.Name, 6) = "Amount" Then
                   ' oBookMark.Range.Text = Format(wsData.Cells(rng2.Row, objX.Column), "£#,##0.00")
                Else
                    oBookMark.Range.Text = wsData.Cells(rng2.Row, objX.Column)
                End If
            Else
                MsgBox "Bookmark '" & oBookMark.Name & "' not found", vbOKOnly + vbCritical, "Error"
                GoTo Tidy_Exit
            End If
        Next oBookMark
        '
        oDoc.SaveAs strWordDocumentName
        oDoc.Close
    Next rng2
    '
    Tidy_Exit:
    On Error Resume Next
    Set oDoc = Nothing
    Set oBookMark = Nothing
    Set objX = Nothing
    Set rng1 = Nothing
    Set rng2 = Nothing
    oApp.Quit
    Set oApp = Nothing
    '
    Set wsData = Nothing
    Set wsControl = Nothing
    Set wb = Nothing
    '
    End Sub

    The problem is like the saved document format is different when compared to the mailmerge template. Instead of creating a new document and pasting it, all I need is mailmerge and save as a new document.

    I am new to VBA and I need to know what are the changes to be made.

    Attached is the sample file with the output in the output folder.

    Can someone help me please

    If you need further details, please post

    Thanks,
    aganesan99
    Attached Files Attached Files
    Last edited by aganesan99; 12-27-2013 at 10:46 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MailMerge
    By turnej in forum Excel General
    Replies: 0
    Last Post: 05-17-2013, 10:20 AM
  2. Mailmerge
    By turnej in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-05-2013, 02:32 AM
  3. vba mailmerge problem
    By roberto21 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-27-2013, 02:55 PM
  4. Mailmerge
    By tmac in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-08-2007, 05:14 PM
  5. mailmerge
    By frank in forum Excel General
    Replies: 1
    Last Post: 02-24-2005, 04:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1