VBA Outlook | VBAコードを使用してOutlookから電子メールを送信する方法は?

VBAがExcelで、マクロの作成でタスクをExcelで自動化する方法を見てきました。MicrosoftOutlookには、VBAのリファレンスもあり、VBAを使用してOutlookを制御できます。これにより、Outlookで繰り返されるタスクの自動化が容易になります。 Excelと同様に、開発者機能がOutlookでVBAを使用できるようにする必要があります。

VBA Outlook

VBAの優れている点は、PowerPoint、Word、Outlookなどの他のMicrosoftオブジェクトを参照できることです。美しいプレゼンテーションを作成したり、Microsoft Word文書を操作したり、最後に電子メールを送信したりすることができます。はい、あなたはそれを正しく聞きました、私たちはExcel自体からメールを送ることができます。これは厄介に聞こえますが、同時に私たちの顔にも笑顔をもたらします。この記事では、VBAコーディングを使用してExcelからMicrosoftOutlookオブジェクトを操作する方法を紹介します。読む…

ExcelからOutlookを参照するにはどうすればよいですか?

Outlookはオブジェクトであり、オブジェクト参照ライブラリでこれへの参照を設定する必要があることを忘れないでください。Outlookオブジェクトを参照するように設定するには、次の手順に従います。

手順1: Visual BasicEditorに移動します。

ステップ2: [ツール]> [参照]に移動します。

手順3:以下の参照オブジェクトライブラリで、下にスクロールして[ MICROSOFT OUTLOOK 14.0OBJECTLIBRARY]を選択します

「MICROSOFTOUTLOOK14.0 OBJECT LIBRARY」のチェックボックスをオンにして、ExcelVBAで使用できるようにします。

これで、ExcelからVBAOutlookオブジェクトにアクセスできます。

ExcelからVBAOutlookからメールを送信するコードを書く

Outlookアプリを介してExcelからメールを送信できます。このために、VBAコードを記述する必要があります。Outlookからメールを送信するには、次の手順に従います。

このVBAOutlook to Excelテンプレートはこちらからダウンロードできます– VBA Outlook toExcelテンプレート

ステップ1:サブプロシージャを作成します。

コード:

 Option Explicit Sub Send_Exails()End Sub 

手順2:変数をVBAOutlook.Applicationとして定義します。

コード:

 Option Explicit Sub Send_Exails()Dim OutlookApp As Outlook.Application End Sub 

ステップ3: VBAOutlookアプリケーションへの上記の変数参照。Outlookでは、電子メールを送信する必要があるため、別の変数をOutlook.MailItemとして定義します。

コード:

 Option Explicit Sub Send_Exails()Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem End Sub 

ステップ4:これで両方の変数がオブジェクト変数になります。それらを設定する必要があります。まずとして、変数「OutlookApp」を設定新しいOutlook.Application

コード:

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

手順5: 2番目の変数「OutlookMail」を次のように設定します。

OutlookMail = OutlookApp.CreateItem(olMailItem)を設定します

コード:

 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 

手順6: Withステートメントを使用してVBA OutlookMailにアクセスします。

コード:

 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 

これで、「メールの本文」、「宛先」、「CC」、「BCC」、「件名」などのメールアイテムで利用可能なすべてのアイテムにアクセスできます。

ステップ7: withステートメントの中に、ドットを入れることでIntelliSenseリストを表示できます。

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 id’s,

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 object library of Excel VBA

By setting the reference to the object 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() 'This code is early binding i.e in Tools > Reference >You have check "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp.CreateItem(olMailItem) With OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Dear ABC" & "

" & "

" & "Please find the attached file" & .HTMLBody 'last .HTMLBody includes signature from the outlook. ''

includes line breaks b/w two lines .To = "[email protected]" .CC = "[email protected]" .BCC = "[email protected];[email protected]" .Subject = "Test mail" .Attachments = ThisWorkbook .Send End With End Sub