Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Const FOLDER_SAVED As String = "<Destination Folder Path>"
  4. Const SOURCE_FILE_PATH As String = "<Data File Path>"
  5.  
  6. Sub TestRun()
  7. Dim MainDoc As Document, TargetDoc As Document
  8. Dim dbPath As String
  9. Dim recordNumber As Long, totalRecord As Long
  10.  
  11. Set MainDoc = ActiveDocument
  12. With MainDoc.MailMerge
  13.  
  14. '// if you want to specify your data, insert a WHERE clause in the SQL statement
  15. .OpenDataSource Name:=SOURCE_FILE_PATH, sqlstatement:="SELECT * FROM [<Worksheet Name>$]"
  16.  
  17. totalRecord = .DataSource.RecordCount
  18.  
  19. For recordNumber = 1 To totalRecord
  20.  
  21. With .DataSource
  22. .ActiveRecord = recordNumber
  23. .FirstRecord = recordNumber
  24. .LastRecord = recordNumber
  25. End With
  26.  
  27. .Destination = wdSendToNewDocument
  28. .Execute False
  29.  
  30. Set TargetDoc = ActiveDocument
  31.  
  32. TargetDoc.SaveAs2 FOLDER_SAVED & .DataSource.DataFields("Client_Name").Value & ".docx", wdFormatDocumentDefault
  33. TargetDoc.ExportAsFixedFormat FOLDER_SAVED & .DataSource.DataFields("Client_Name").Value & ".pdf", exportformat:=wdExportFormatPDF
  34.  
  35. TargetDoc.Close False
  36.  
  37. Set TargetDoc = Nothing
  38.  
  39. Next recordNumber
  40.  
  41. End With
  42.  
  43. Set MainDoc = Nothing
  44. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement