Outlook de VBA - ¿Cómo enviar correos electrónicos desde Outlook usando el código VBA?

Tabla de contenido

Hemos visto VBA en Excel y cómo automatizamos nuestras tareas en Excel con la creación de macros, en Microsoft Outlook también tenemos una referencia para VBA y con la cual podemos controlar Outlook usando VBA, esto hace que nuestras tareas repetidas en Outlook sean más fáciles de automatizar, y similar a Excel, necesitamos habilitar la función de desarrollador para usar VBA en Outlook.

Outlook de VBA

La belleza de VBA es que podemos hacer referencia a otros objetos de Microsoft como PowerPoint, Word y Outlook. Podemos crear hermosas presentaciones. Podemos trabajar con documentos de Microsoft Word y, finalmente, también podemos enviar los correos electrónicos. Sí, has escuchado bien. Podemos enviar correos electrónicos desde Excel. Esto suena incómodo pero al mismo tiempo pone una sonrisa en nuestro rostro. En este artículo, le mostraré cómo trabajar con el objeto de Microsoft Outlook desde Excel usando la codificación VBA. Sigue leyendo …

¿Cómo hacemos referencia a Outlook desde Excel?

Recuerde, Outlook es un objeto y necesitamos establecer la referencia a esto en la biblioteca de referencia de objetos. Para configurar el objeto de Outlook como referencia, siga los pasos a continuación.

Paso 1: vaya al Editor de Visual Basic.

Paso 2: Vaya a Herramientas> Referencia.

Paso 3: En las referencias siguientes, biblioteca de objetos, desplácese hacia abajo y seleccione "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY".

Marque la casilla de “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY” para que esté disponible para Excel VBA.

Ahora podemos acceder al objeto de Outlook de VBA desde Excel.

Escriba un código para enviar correos electrónicos desde VBA Outlook desde Excel

Podemos enviar los correos electrónicos desde Excel a través de la aplicación Outlook. Para esto, necesitamos escribir códigos VBA. Siga los pasos a continuación para enviar los correos electrónicos desde Outlook.

Paso 1: cree un subprocedimiento.

Código:

Opción Sub explícito Send_Exails () End Sub

Paso 2: Defina la variable como VBA Outlook.Application .

Código:

Opción Explicit Sub Send_Exails () Atenuar OutlookApp como Outlook.Application End Sub

Paso 3: La variable anterior hace referencia a la aplicación VBA Outlook. En Outlook, necesitamos enviar correos electrónicos, así que defina otra variable como Outlook.MailItem.

Código:

Opción Explicit Sub Send_Exails () Atenuar OutlookApp como Outlook.Application Atenuar OutlookMail como Outlook.MailItem End Sub

Paso 4: Ahora, ambas variables son variables de objeto. Necesitamos configurarlos. Primero, configure la variable "OutlookApp" como Nueva aplicación de Outlook .

Código:

Sub Send_Exails () Atenuar OutlookApp como Outlook.Application Atenuar OutlookMail como Outlook.MailItem Establecer OutlookApp = New Outlook.Application End Sub

Paso 5: Ahora, configure la segunda variable, "OutlookMail", como se muestra a continuación.

Establecer OutlookMail = OutlookApp.CreateItem (olMailItem)

Código:

Sub Send_Exails() Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp.CreateItem(olMailItem) End Sub

Step 6: Now using With statement access VBA Outlook Mail.

Code:

Sub Send_Exails() Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp.CreateItem(olMailItem) With OutlookMail End With End Sub

Now we can access all the items available with email items like “Body of the email,” “To,” “CC,” “BCC,” “Subject,” and many more things.

Step 7: Now, inside the with the statement, we can see the IntelliSense list by putting a dot.

Step 8: First, select the body format as olFormatHtml.

Code:

With OutlookMail .BodyFormat = olFormatHTML End With

Step 9: Now display the email.

Code:

With OutlookMail .BodyFormat = olFormatHTML .Display End With

Step 10: Now, we need to write the email in the body of the email. For this, select HtmlBody.

Code:

With OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Write your email here" End With

Below is the example of the body of the email writing.

Step 11: After writing the email, we need to mention the email id of the receiver. For this access, “To.”

Step 12: Next, mention for whom you want to CC the email.

Step 13: Now mention the BCC email ids,

Step 14: Next thing is we need to mention the subject for the email we are sending.

Step 15: Now add attachments. If you want to send the current workbook as an attachment, then use the attachment as This workbook.

Step 16: Finally, send the email by using the Send method.

Now, this code will send the email from your VBA outlook mail. Use the below VBA code to send emails from your outlook.

To use the below code, you must set the object reference to “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY” under the object library of Excel VBA.

By setting the reference to the object, the library is called early binding. The reason why we need to set the reference to object library because without setting the object library as “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY,” We cannot access the IntelliSense list of VBA properties and methods. This makes the writing of code difficult because you need to be sure of what you are writing in terms of technique and spellings.

Sub Send_Emails () 'Este código es de enlace temprano, es decir, en Herramientas> Referencia> Ha marcado "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" Atenuar OutlookApp como Outlook.Application Atenuar OutlookMail como Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp. CreateItem (olMailItem) con OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Estimado ABC" & "
" & "
" & "Por favor, busque el archivo adjunto" & .HTMLBody 'último .HTMLBody incluye la firma de la perspectiva. ''
incluye saltos de línea b / n dos líneas .To = "[email protected]" .CC = "[email protected]" .BCC = "[email protected]; [email protected]" .Subject = " Correo de prueba ".Attachments = ThisWorkbook .Send End With End Sub

Articulos interesantes...