LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

Excel-Outlookマクロ

Last updated at Posted at 2023-08-02

Excelメール作成マクロ

以下の手順で、Outlookライブラリを有効にします。

  • VB Editorを表示 ExcelでAlt + F11
  • [ツール] > [参照設定]を選択
  • 表示された[参照設定 - VBAProject]
    [参照可能なライブラリ ファイル]一覧で
    [Microsoft Outlook 1x.0 Object Library]のチェックをオン
  • OKを押して完了

Excelのシートの値などを用いて、メールを作成します。

Sub Mail()
  Dim OutlookAP As Outlook.Application
  Dim MailOutlook As Outlook.MailItem
  Set OutlookAP = CreateObject("Outlook.Application")
  Set MailOutlook = OutlookAP.CreateItem(olMailItem)

  With MailOutlook
    .To = Range("A1").Value
    .CC = Range("A1").Value
    .Subject = Range("A1").Value
    .BodyFormat = olFormatPlain
    .Body = "宛先各位" & vbCr & "お疲れ様です" & Range("A1").Value
    '.Display メール内容を表示
    '.Send メール送信
  End With

  Set OutlookAP = Nothing
  Set MailOutlook = Nothing
End Sub

vbCrは改行です。空白行を入れたいならこれを何度も&で繋げばOK
上記の関数を、図形に登録するなりで実行すると、作成されたメールが開きます。
Outlookを起動しておくと良いです。
コメントアウトしてますが、.Sendで、いきなり送信もできます。非推奨

Excelメール既読マクロ

Outlookライブラリが有効である前提。
Outlookを起動しておくと良いです。

Sub MarkAsRead()
  ' Outlookの機能にアクセスするためのMAPIオブジェクトを取得
  Dim ns As Outlook.Namespace
  Set ns = Outlook.Application.GetNamespace("MAPI")

  ' 受信トレイのフォルダオブジェクトを取得
  Dim myFld As Outlook.Folder
  Set myFld = ns.GetDefaultFolder(olFolderInbox)

  ' メールアイテムを取得する
  Dim myItem As Object
  For Each myItem In myFld.Items
    ' 会議メールなどは除外、通常のメールで未読のみ
    If TypeName(myItem) = "MailItem" And myItem.UnRead = True Then
      If myItem.Subject Like "既読にしたいタイトル接頭*" Then
        myItem.UnRead = False
      End If
    End If
  Next
  MsgBox ("既読にしました")
End Sub
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