Automating Mail Merge using Excel VBA

前端 未结 1 981
日久生厌
日久生厌 2020-12-16 09:18

I created a macro in Excel where I can mail-merge data from Excel into Word Letter Template and save the individual files in the folder.

I have Employee data in Exce

相关标签:
1条回答
  • 2020-12-16 09:22

    To save the file in pdf format use

    objWord.ActiveDocument.ExportAsFixedFormat cDir & NewFileName, _
                      ExportFormat:=wdExportFormatPDF
    

    It looks to me that when you are executing the mail merge, it should create a file with ALL of the letters, so when you open it, it would appear that the first letter is the one that is getting saved, but if you scroll down the word file that you have saved, you may find each letter on a new page.

    Instead, you want to execute the merge one letter at a time.
    To fix this, change the lines as follows:

    With .DataSource
      .FirstRecord = r-1
      .LastRecord = r-1
      .ActiveRecord = r-1
    

    You need to use r-1 because Word is going to use the record number in its dataset, and since the data starts in row 2, and the counter r is related to the row, you need r-1.

    You don't need to open up word each time, so put all of the code setting the datasource of the mail merge and creating the word doc outside of your main loop.

    Const WTempName = "letter.docx" 'This is the 07/10 Word Templates name,  
    Dim NewFileName As String
    
    ' Setup directories
    cDir = ActiveWorkbook.path + "\" 'Change if appropriate
    ThisFileName = ThisWorkbook.Name
    
    On Error Resume Next
    
    ' Create a Word Application instance
    bCreatedWordInstance = False
    Set objWord = GetObject(, "Word.Application")
    
    If objWord Is Nothing Then
      Err.Clear
      Set objWord = CreateObject("Word.Application")
      bCreatedWordInstance = True
    End If
    
    If objWord Is Nothing Then
        MsgBox "Could not start Word"
        Err.Clear
        On Error GoTo 0
        Exit Sub
    End If
    
    ' Let Word trap the errors
    On Error GoTo 0
    
    ' Set to True if you want to see the Word Doc flash past during construction
    objWord.Visible = False
    
    'Open Word Template
    Set objMMMD = objWord.Documents.Open(cDir + WTempName)
    objMMMD.Activate
    
    'Merge the data
    With objMMMD
    .MailMerge.OpenDataSource Name:=cDir + ThisFileName, _
        sqlstatement:="SELECT *  FROM `Data$`"   ' Set this as required
    
    For r = 2 To lastrow
        If Cells(r, 7).Value = "Letter Generated Already" Then GoTo nextrow
    'rest of code goes here
    

    Also, instead of checking the Excel file for the Employee name to create the file name, you could do this after you merge the document. For me, this is a little more intuitive to link the file name to the letter you have just merged. To do this update the line further to:

    With .DataSource
      .FirstRecord = r-1
      .LastRecord = r-1
      .ActiveRecord = r-1
      EmployeeName = .EmployeeName 'Assuming this is the field name
    

    Then immediately before saving the file you can do this:

     ' Save new file
    NewFileName = "Offer Letter - " & EmployeeName & ".docx"
    objWord.ActiveDocument.SaveAs cDir + NewFileName
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题