PDA

View Full Version : Solved: Split into separate files as PDF with a field name in mail merging



a_chaitanya
06-07-2013, 01:19 AM
Hi Team,

I tried below the code, took from many sources and many statements in the code here. The problem with the below code is, it generating only first record as .PDF file with desired field as file name and generating many new documents as per the records and asking for to save each document.

Please help me in generating the number of .PDF files with the desired EMP_ID field as the file name as per the number of records in the merge file attached. Using Microsoft Word 2007.

Sub Splitter()
Dim strEmpID As String
Dim strPath As String
Dim Source As Document

strPath = ThisDocument.Path
With ThisDocument
strPath = .Path
With .MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True

Selection.EndKey Unit:=wdStory
numlets = Selection.Information(wdActiveEndSectionNumber)
If numlets > 1 Then numlets = numlets - 1
Selection.HomeKey Unit:=wdStory
'BaseName = "p:\cbalaji$\Desktop\Chaitanya_all_14062011\Macros\"
With .DataSource
strEmpID = .DataFields("Emp_ID").Value

For Counter = 1 To numlets
docName = strPath & "\" & strEmpID
'strPath & Right("0" & LTrim(Str(Counter)), 10)
ActiveDocument.Sections.First.Range.Cut

Documents.Add
Selection.Paste
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveDocument.SaveAs FileName:=docName & ".pdf", _
FileFormat:=wdFormatPDF

Next Counter
End With
ActiveWindow.Close SaveChanges = False
End With
End With
End Sub

Thanks & Regards

macropod
06-07-2013, 07:14 AM
Cross-posted (and claimed by you to have been solved) at: http://www.excelforum.com/word-programming-vba-macros/928382-split-into-separate-files-as-pdf-with-a-field-name-in-mail-merging.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

a_chaitanya
06-08-2013, 08:34 PM
Hi Macropod,

Thanks for the rules. Sure, will follow the same.

Chaitanya