本日は、ExcelVBAを使用して、outlookからメールを送信する方法をご紹介いたします。
ExcelVBAからメール送信を行うメリットとしてExcel内に宛先や件名、本文などを書き込み
VBAでまとめて送信ということも可能になります。
Outlookには、送受信できるアカウントを事前に準備しておきます。
Outlookでメールを送るVBA
コードを解説します。
Outlook.Applicationオブジェクトを使用してOutlookを開きます。
Dim oApp As Outlook.Application
Set oApp = New Outlook.Application
また、「ツール」→「参照設定」で「Microsoft Outlook Object Library 」にチェックを入れておきます。


次に、メールアイテムを作成し、必要な情報を設定します。
メールアドレス、件名、本文を指定できます。
Dim oMail As Outlook.MailItem
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = "送信先メールアドレス"
.Subject = "件名"
.Body = "本文"
End With
Excelからメールを送信する場合、メールアドレスや件名や本文をExcelのセルに書き込み
VBAで内容を取得します。
Dim toAddress As String
Dim subject As String
Dim body As String
toAddress = Range("A2").Value ' A2セルに送信先アドレス
subject = Range("B2").Value ' B2セルに件名
body = Range("C2").Value ' C2セルに本文
Dim oMail As Outlook.MailItem
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = toAddress
.Subject = subject
.Body = body
End With
最後に、メールを送信するために、以下のようにSendメソッドを使用します。
oMail.Send
コード全文です。
Sub SendEmail()
Dim oApp As Outlook.Application
Set oApp = New Outlook.Application
Dim toAddress As String
Dim subject As String
Dim body As String
toAddress = Range("A1").Value
subject = Range("A2").Value
body = Range("A3").Value
Dim oMail As Outlook.MailItem
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = toAddress
.Subject = subject
.Body = body
End With
oMail.Send
Set oMail = Nothing
Set oApp = Nothing
End Sub
Excel VBAOutlookでメール送信を実行
では、VBAを実行します。
Excelには下記のように記載しています。

実行後、Outlookの送信済みトレイにメールがあります。

Gmailにも届いていました。

また、構造体の変数の中身を確認する方法として、for文でLBoundとUBound関数を使うことで一気に読みだせます。
Dim j As Integer '
For j = LBound(arr) To UBound(arr)
MsgBox myArray(j).Field1 & myArray(j).Field2 & myArray(j).Field3
Next j
End Sub
まとめ
本日は、Excel VBAを使用してOutlookでメール送信する方法の解説でした。