LoginSignup
0
2

More than 3 years have passed since last update.

Outlookの新規メールをExcelマクロ(またはVBScript)で作る

Posted at

これは何かというと

Excelマクロの実行結果を、Outlookの新規メールの形で出力したい、
ある日、そんな思いに駆られる出来事がありまして、
その時に作ったExcelVBAのコードの一部分です。

その後、よく考えたら、
そもそもExcelマクロである必要もなくVBScriptで充分、ということになりまして、
Excel/VBScript兼用に仕立て直しました。

あれこれ調べながら悩みながら作っていって、出来上がったら意外とシンプルでした。
なぁんだ、という感じ。でもせっかくなので共有します。

コード

Function NewMail(pSubject, pTo, pBody) 'As Outlook.MailItem
    Set NewMail = CreateObject("Outlook.Application").CreateItem(0) '  olMailItem: 0
    With NewMail
        .BodyFormat = 1  '  olFormatPlain: 1
        .Subject = pSubject
        .To = pTo
        .Body = pBody
        .Display
    End With
End Function

VBScriptでの利用イメージ

NewMail.vbs
'使い方その1: NewMail.vbs をダブルクリック
'使い方その2: honbun.txt のアイコンをつまんで NewMail.vbs の上にドロップ

Option Explicit
Const KENMEI = "todo", ATESAKI = "ashika@example.com"

NewMail KENMEI, ATESAKI, yomikomi(WScript.Arguments)


Function yomikomi(args)
    If args.Count > 0 Then yomikomi = GetText(args.Item(0))
End Function

Function GetText(f)
    With CreateObject("ADODB.Stream")
        .Charset = "UTF-8"
        .Open
        .LoadFromFile f
        GetText = .ReadText
        .Close
    End With
End Function
0
2
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
2