0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Outlook VBAで宛先(To)から挨拶文を自動生成する

Posted at

はじめに

OutlookでEメールを作成する際、だいたいは決まった挨拶文を書くかと思います。例えば、

To: ◇◇地域振興局建設部 △△課 ○○班 猪鹿技師 様 <inosika.chouko@pref.cats.lg.jp>

に対して、

◇◇地域振興局 建設部
△△課 ○○班
 猪鹿 技師 様

いつも大変お世話になっております。

といった感じに。テンプレートにしている方もいるかと思いますが、多くの人は毎回同じことを手入力したり、以前の送信済メールからコピペしているのではないでしょうか。

連絡先から宛先(To, CC, BCC)に相手のアドレスを入れたときに”挨拶文”を自動で入力できたら便利だと思ったので、これをOutlook VBAで実装してみたいと思います。 (※「自動で」と書きましたが、宛先を入力したときのイベント処理が分からなかったため、ここでは宛先を入れた後にリボンやクィックアクセスツールバーに置いたボタンから起動することとします。)

基本的な考え方

用語

  • メインのウィンドウ : Explorer オブジェクト
  • それ以外のウィンドウ : Inspector オブジェクト
  • 宛先(To,CC,BCC) : Recipients コレクション
  • 個々の宛先 : Recipient オブジェクト
  • 連絡先ユーザ : AddressEntry オブジェクト
  • 連絡先アイテム : ContactItem オブジェクト

前提条件

今回は、基本的な処理の説明ですので、以下の前提条件を付けておきます。

  1. 「宛先」(メールアドレスや氏名、勤務先の情報)は、Outlookの「連絡先」に必ず入っているものとする
  2. 新規メッセージ作成ウィンドウでの作業とする (Inspectorオブジェクト)
  3. メッセージの形式は「テキスト形式」とする (PlainText形式)
  4. 対象とする宛先は To に一つのみとする
  5. 作成ウィンドウの宛先(To)に対象のメールアドレスを入力しておく

outlook-vba-recipient-to-headline-01.jpg

処理の流れ

基本的な処理の流れは以下の通りです。(確認やエラーの処理は省きます)

  1. MailItemオブジェクトを取得する
  2. 宛先 (Recipientsコレクション) を取得し、.Type が olTo だった場合の Index を取得する
  3. 本文の内容 (MailItem.Body) を取得しておく
  4. Index に対応する宛先 (Recipientオブジェクト) に対応する連絡先ユーザ (AddressEntryオブジェクト) から 連絡先アイテム (ContactItemオブジェクト) を取得する
  5. ContactItem から 勤務先、部署、姓(名字)、役職の各プロパティを取得して挨拶文を生成する
  6. 3で取得しておいた本文の先頭に挨拶文をつなげて本文を上書きする
Outlook VBA
Public Sub SetEMailHeadline()

  '// 変数の定義
  Dim myMailItem    As MailItem
  Dim myRecipient   As Recipient
  Dim myContactItem As ContactItem
  Dim myIndex       As Long
  Dim tempBodyText  As String
  Dim tempHeadline  As String

  '// 1. MailItemの取得
  Set myMailItem = Application.ActiveInspector.CurrentItem

  '// 2. RecipientsコレクションからToを選んでIndexを取得
  For Each myRecipient In myMailItem.Recipients
    If myRecipient.Type = olTo Then
      myIndex = myRecipient.Index
    Exit For
  Next

  '// 3. 本文の内容を取得
  tempBodyText = myMailItem.Body

  '// 4. 宛先に該当する連絡先アイテムを取得
  Set myContactItem = myMailItem.Recipients.Item(myIndex).AddressEntry.GetContact

  '// 5. 各種プロパティを取得
  With myContactItem
    tempHeadline = .CompanyName & vbCrLf & _
                   .Department & vbCrLf & _
                   "  " & .LastName & " " & .JobTitle & " 様" & vbCrLf & _
                   vbCrLf & _
                   "いつも大変お世話になっております。"
  End With

  '// 6. 挨拶文を本文の先頭に追加
  myMailItem.Body = tempHeadline & tempBodyText

  '// オブジェクトの解放
  Set myContactItem = Nothing
  Set myMailItem = Nothing
End Sub

おわりに

上記のコードでは、考えられる条件分岐やエラー処理などをしていません。次回はそこら辺を詰めていきたいと思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?