OutlookのCOMアドインを、Visual Studio Codeと.NET Frameworkで開発してみました。
通常、COMアドインの開発にはVisual Studio(とVSTO)が使われますが、今回はあえてVisual Studio Codeと.NET Frameworkを使って開発してみました。
処理内容
- メール送信時にメッセージを表示します
- Guidはpowershellで生成して
[Guid("GUID")]
へ入れます -
[ProgId("OutlookCOMAddin.Connect")]
はレジストリ登録時にFriendlyName
と合わせます
[guid]::NewGuid()
ClassLib.cs
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Extensibility;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookCOMAddin
{
[ComVisible(true)]
// [Guid("GUID")] ←GUIDを入れる
[ProgId("OutlookCOMAddin.Connect")]
public class Connect : IDTExtensibility2
{
private Outlook.Application outlookApp;
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
outlookApp = (Outlook.Application)application;
outlookApp.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(OnItemSend);
}
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
{
if (outlookApp != null)
{
outlookApp.ItemSend -= new Outlook.ApplicationEvents_11_ItemSendEventHandler(OnItemSend);
outlookApp = null;
}
}
public void OnAddInsUpdate(ref Array custom) { }
public void OnStartupComplete(ref Array custom) { }
public void OnBeginShutdown(ref Array custom) { }
private void OnItemSend(object item, ref bool cancel)
{
if (item is Outlook.MailItem mail)
{
MessageBox.Show(
"メールを送ります。",
"送信確認",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}
プロジェクトの設定
csprojのポイント
-
TargetFramework
はnet48
に設定 - 参照を追加
-
Extensibility
Microsoft.Office.Interop.Outlook
- Office/Outlookをインストールしていれば入っているはずのものです
-
- Privateはfalseにしてビルド時にコピーしない
- Extensibility,dll Microsoft.Office.Interop.Outlook.dllをビルド時に含めて再配布するとライセンス違反になるので注意
- HintPathはビルド時用のため
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<!-- <Nullable>enable</Nullable> -->
<Version>0.1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<Reference Include="Extensibility">
<HintPath>Package\extensibility.dll</HintPath>
<!-- ビルド時にdllを含めるか -->
<Private>false</Private>
</Reference>
<Reference Include="Microsoft.Office.Interop.Outlook">
<HintPath>Package\Microsoft.Office.Interop.Outlook.dll</HintPath>
<!-- ビルド時にdllを含めるか -->
<Private>false</Private>
</Reference>
<Reference Include="System.Windows.Forms" />
</ItemGroup>
</Project>
-
dotnet publish -c release
で出力して完成です -
次回はInno Setupを使いインストーラーの作成を行う予定です
⇒【インストーラー編】VSCodeでOutlookのCOMアドインをつくる
Outlookの新旧とCOMアドインの今後
- 新しいOutlook/Outlook(New)ではCOMアドインが非対応となり、WEBアドインへの移行が推奨されています
- 今回のようなCOMアドインの開発は今後の移行にも注意が必要になります
- Outlook2024が2029/10/9までサポートされるのでOutlook(Classic)もそれまでは使えるでしょうが、その後は分からないですね