You are on page 1of 3

Hay que tener preparados tres documentos en total:

- Word realizado con la herramienta de combinar correspondencia, que ser el cuerpo del
mensaje. En este ejemplo es el documento email.
- Excel con todos los campos que se quieran combinar en el mail; este documento da igual que
est abierto o cerrado a la hora de pasar la macro. En este ejemplo es el documentomailmerge.
- Word con una tabla SIN ENCABEZADOS que contenga en una columna el e-mail y en las
restantes las rutas correspondientes a los archivos que se quieran adjuntar; este documento se
debe cerrar antes de pasar la macro. En este ejemplo es el documento Directorio1.
Tenis el texto de la macro en el documento macro, para escribirlo tenis que ir a la pestaa
"Vista", botn "Macros", darle a "Crear" y hacer una que no tenga nada de nada, le ponis el
nombre que queris, luego se va a cambiar por "adjuntararchivos". Una vez creada la macro
que no tiene nada le dais otra vez a "Macros" y "Modificar", eligiendo la que acabamos de
crear. Se abrir el editor de Visual Basic, pero no asustarse. Simplemente copiad y pegad el
texto que os doy, y ya la tenis.
Los pasos a seguir son:
1. En el Word con el cuerpo del mensaje, escribir el texto del mail con todos los campos
combinados que se quiera.
2. Finalizar y combinar --> Editar documentos individuales
3. En el documento que se abre al hacer el paso anterior, ejecutar la macro que se llama
adjuntararchivos
4. En el primer cuadro de dilogo, buscar el documento Word con el listado de mails y adjuntos
para que lo abra.
5. En el segundo cuadro de dilogo, poner el asunto de los mails que se van a enviar y aceptar
ojo, el mismo asunto para todos, no puede tener campos combinados!!
Saldr un cuadro con el nmero de mensajes que se han enviado.

Sub adjuntararchivos()
Dim Source As Document, Maillist As Document, TempDoc As Document
Dim Datarange As Range
Dim i As Long, j As Long
Set objEmail = CreateObject("CDO.Message")
objEmail.from = "it.iberica@esprinet.com"
Dim bStarted As Boolean
Set oOutlookApp = CreateObject("Outlook.Application")
' Dim oItem As Outlook.MailItem
Dim mysubject As String, message As String, title As String
Set Source = ActiveDocument
' Check if Outlook is running. If it is not, start Outlook
On Error Resume Next
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
' Open the catalog mailmerge document
With Dialogs(wdDialogFileOpen)
.Show
End With
Set Maillist = ActiveDocument
' Show an input box asking the user for the subject to be inserted into the email messages
message = "Enter the subject to be used for each email message."
title = " Email Subject Input"

' Set prompt.

' Set title.

' Display message, title


mysubject = InputBox(message, title)
' Iterate through the Sections of the Source document and the rows of the catalog mailmerge
document,
' extracting the information to be included in each email.

For j = 1 To Source.Sections.Count - 1
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
.Subject = mysubject
.Body = Source.Sections(j).Range.Text
Set Datarange = Maillist.Tables(1).Cell(j, 1).Range
Datarange.End = Datarange.End - 1
.To = Datarange
For i = 2 To Maillist.Tables(1).Columns.Count
Set Datarange = Maillist.Tables(1).Cell(j, i).Range
Datarange.End = Datarange.End - 1
.Attachments.Add Trim(Datarange.Text), olByValue, 1
Next i
.Send
End With
Set oItem = Nothing
Next j
Maillist.Close wdDoNotSaveChanges
' Close Outlook if it was started by this macro.
If bStarted Then
oOutlookApp.Quit
End If
MsgBox Source.Sections.Count - 1 & " messages have been sent."
'Clean up
Set oOutlookApp = Nothing
End Sub

You might also like