Hi, I am working in a mail merge document and I'm trying to create a macro that allows to select the recipient list, currently I have to type the path, this is my code:
Code:
Sub Autocombineta()
Dim URL As String
URL = InputBox("¿Donde desea crear los documentos? agregar \ al final de la ruta")
Dim SOURCE_FILE_PATH As String
SOURCE_FILE_PATH = InputBox("¿Elija la lista de destinatarios")
Dim MainDoc As Document, TargetDoc As Document
Dim dbPath As String
Dim recordNumber As Long, totalRecord As Long
Set MainDoc = ActiveDocument
With MainDoc.MailMerge
'Indicar entre paréntesis la solapa de excel que contiene los datos, poner el signo $ al final'
.OpenDataSource Name:=SOURCE_FILE_PATH, SQLStatement:="SELECT * FROM [Prueba$]"
totalRecord = .DataSource.RecordCount
For recordNumber = 1 To totalRecord
With .DataSource
.ActiveRecord = recordNumber
.FirstRecord = recordNumber
.LastRecord = recordNumber
End With
.Destination = wdSendToNewDocument
.Execute False
Set TargetDoc = ActiveDocument
TargetDoc.ExportAsFixedFormat URL & .DataSource.DataFields("Encabezado").Value & ".pdf", exportformat:=wdExportFormatPDF
TargetDoc.Close False
Set TargetDoc = Nothing
Next recordNumber
End With
Set MainDoc = Nothing
End Sub