0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Power Automate DesktopでOutlookの下書きメールを作成する

0
Last updated at Posted at 2026-03-14

概要

Power Automate Desktopで Outlook の下書きメールを作成し、.msg 形式で指定のフォルダに保存する処理を実装したいケースがあります。

PAD には Outlook を操作する標準アクションがありますが、下書きメールを .msg ファイルとして任意のフォルダに保存するという要件を標準アクションだけで実現しようとすると構成がやや複雑になります。

本記事では、「.NET スクリプト実行(VB.NET)」アクションを利用し、Outlook の下書きメールを作成して指定フォルダに保存する方法を紹介します。
※Power Fxを有効化した状態で説明しています。

前提条件

本記事の手順を実行するには、以下の条件を満たしている必要があります。

  • Outlook がインストールされていること
  • Outlook を一度手動で起動し、プロファイル設定が完了していること
  • 下書きメールを保存する フォルダが事前に存在していること

実装概要

「.NET スクリプト実行」アクションを使用し、言語には「VB.NET」を指定します。
本実装では、以下を行います。

  1. Outlook を COM(Late Binding)で起動
  2. 下書きメール(MailItem)を作成
  3. 宛先(To / CC / BCC)・添付ファイルを設定
  4. 本文を HTML 形式で設定(改行を<br/> に変換)
  5. msg 形式で指定フォルダに保存
' 保存先フォルダとファイル名を結合し、ファイルのフルパスを作成
Dim strSaveAsFile As String = System.IO.Path.Combine(InDraftMailFolderPath, InMailFileName & ".msg")

' Outlook 起動(Interop参照なし / Late Binding)
Dim outlookType As Type = Type.GetTypeFromProgID("Outlook.Application")
Dim App As Object = Activator.CreateInstance(outlookType)

' MailItem 作成(0 = olMailItem)
Dim MailMessage As Object = App.CreateItem(0)

' HTML形式(2 = olFormatHTML)
MailMessage.BodyFormat = 2

' 添付ファイルの追加
If InAttachmentFile IsNot Nothing Then
    For Each strAttachFile As String In InAttachmentFile
        If Not String.IsNullOrWhiteSpace(strAttachFile) Then
            If System.IO.File.Exists(strAttachFile) Then
                MailMessage.Attachments.Add(strAttachFile)
            End If
        End If
    Next
End If

' Toの設定(Recipient.Type: 1 = olTo)
If InTo IsNot Nothing Then
    For Each strTo As String In InTo
        If (strTo.Length > 10) Then
            Dim recipTo As Object = MailMessage.Recipients.Add(strTo)
            recipTo.Type = 1
        End If
    Next
End If

' CCの設定(Recipient.Type: 2 = olCC)
If InCC IsNot Nothing Then
    For Each strCC As String In InCC
        If (strCC.Length > 10) Then
            Dim recipCc As Object = MailMessage.Recipients.Add(strCC)
            recipCc.Type = 2
        End If
    Next
End If

' BCCの設定(Recipient.Type: 3 = olBCC)
If InBCC IsNot Nothing Then
    For Each strBCC As String In InBCC
        If (strBCC.Length > 10) Then
            Dim recipBCc As Object = MailMessage.Recipients.Add(strBCC)
            recipBCc.Type = 3
        End If
    Next
End If

' アドレス解決
MailMessage.Recipients.ResolveAll()

' 本文(HTML)の生成
Dim html As String = InBody

' HTMLエンティティをデコード(&lt;b&gt; → <b> など)
html = System.Net.WebUtility.HtmlDecode(html)

' 改行(CRLF / LF / CR)を <br/> に変換
html = System.Text.RegularExpressions.Regex.Replace(html, "\r\n|\r|\n", "<br/>")

' 件名・本文を設定
MailMessage.HTMLBody = html
MailMessage.Subject = InSubject

' .msg 形式で保存
MailMessage.SaveAs(strSaveAsFile)

' COM オブジェクト解放
System.Runtime.InteropServices.Marshal.ReleaseComObject(App)

スクリプトパラメーター設定

以下のように 入力値(In) を設定します。
(例:件名、本文、宛先配列、添付ファイル配列、保存先フォルダなど)

実行結果

フローを実行すると、指定した保存先フォルダに .msg ファイルが生成されます。
ファイルの種類は 「Outlook アイテム」となり、ダブルクリックすると Outlook の下書きメールとして正しく開くことができます。

補足

  • 本実装では HTML 形式の本文を扱っているため、改行はそのままでは反映されず、<br/> への変換が必要です
  • Outlook Interop の参照が使えない環境でも、Late Binding によって .msg 保存を実現できます
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?