Guest User

Untitled

a guest
Jul 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. Sub RunMerge()
  2.  
  3. Dim wd As Object
  4. Dim wdocSource As Object
  5. Dim Last_Row As Integer
  6.  
  7. Dim strWorkbookName As String
  8.  
  9. Last_Row = Worksheets("_Main").Cells(52, 6).Value
  10.  
  11. Const wdFormLetters = 0, wdOpenFormatAuto = 0
  12. Const wdSendToNewDocument = 0, wdDefaultFirstRecord = 1, wdDefaultLastRecord = -16
  13.  
  14. On Error Resume Next
  15. Set wd = GetObject(, "Word.Application")
  16. If wd Is Nothing Then
  17. Set wd = CreateObject("Word.Application")
  18. End If
  19. On Error GoTo 0
  20.  
  21. Set wdocSource = wd.Documents.Open("C:UsersevieraDesktopCertificateTemplateCertificateTemplate_Re-entryV1.docx")
  22.  
  23. strWorkbookName = ThisWorkbook.Path & "" & ThisWorkbook.Name
  24.  
  25. wdocSource.MailMerge.MainDocumentType = wdFormLetters
  26.  
  27. wdocSource.MailMerge.OpenDataSource _
  28. Name:=strWorkbookName, _
  29. AddToRecentFiles:=False, _
  30. Revert:=False, _
  31. Format:=wdOpenFormatAuto, _
  32. Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
  33. SQLStatement:="SELECT * FROM `_Main$`"
  34.  
  35. With wdocSource.MailMerge
  36. .Destination = wdSendToNewDocument
  37. .SuppressBlankLines = True
  38. With .DataSource
  39. .FirstRecord = wdDefaultFirstRecord
  40. .LastRecord = wdDefaultLastRecord
  41. End With
  42. .Execute Pause:=False
  43. End With
  44.  
  45. wd.Visible = True
  46. wdocSource.Close SaveChanges:=False
  47.  
  48. Set wdocSource = Nothing
  49. Set wd = Nothing
  50.  
  51. End Sub
  52.  
  53. With wdocSource.MailMerge
  54. .Destination = wdSendToNewDocument
  55. .SuppressBlankLines = True
  56. With .DataSource
  57. .FirstRecord = wdDefaultFirstRecord
  58. .LastRecord = wdDefaultLastRecord
  59. End With
Add Comment
Please, Sign In to add comment