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
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