Outlook VBAによるメール作成の基礎
Outlook VBAの情報はそれほど多くないため、メール作成に限定して基礎となる情報を共有します。
メールオブジェクトを生成
Dim mail As MailItem: Set mail = CreateItem(olMailItem)
メールのInspectorを表示
mail.Display
送信アカウントを指定
アカウント名で指定
mail.SendUsingAccount = Session.Accounts("Account Name")
アカウント名をメールアドレスで指定
Dim acco As Account For Each acco In Application.Session.Accounts If acco.CurrentUser.Address = "user@example.local" Then mail.SendUsingAccount = acco: Exit For End If Next acco
Subject
mail.Subject = "Mail title"
To
mail.To = "test1@example.local; test2@example.local; test3 <test3@example.local>"
CC
mail.CC = "test1@example.local; test2@example.local; test3 <test3@example.local>"
BCC
mail.BCC = "test1@example.local; test2@example.local; test3 <test3@example.local>"
各フォーマットによるメール作成
テキスト
mail.BodyFormat = olFormatPlain mail.Body = "test"
HTML
mail.BodyFormat = olFormatHTML mail.HTMLBody = "<b>test</b>"
RTF
mail.BodyFormat = olFormatRichText ' RTF表記は複雑のためここでは省略します。 ' なお、RTFはOffice 365/2019ではUTF-32のバイト列であることを確認しています。
添付ファイル
追加
mail.Attachments.Add "c:\test\sample.txt", olByValue mail.Attachments.Add "c:\test\sample.msg", olEmbeddeditem
削除
mail.Attachments.Remove 1
下書き保存
mail.Save
送信
mail.Send