Thread: [Solved] Choose recipient list
View Single Post
 
Old 11-16-2020, 04:59 AM
Arwydrunk Arwydrunk is offline Windows 10 Office 2013
Novice
 
Join Date: Nov 2020
Posts: 1
Arwydrunk is on a distinguished road
Default Choose recipient list

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

Last edited by macropod; 11-16-2020 at 12:37 PM. Reason: Added code tags
Reply With Quote