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?

【dll編】VSCodeでOutlookのCOMアドインをつくる

Last updated at Posted at 2025-09-26

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のポイント

  • TargetFrameworknet48に設定
  • 参照を追加
    • 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>

Outlookの新旧とCOMアドインの今後

  • 新しいOutlook/Outlook(New)ではCOMアドインが非対応となり、WEBアドインへの移行が推奨されています
  • 今回のようなCOMアドインの開発は今後の移行にも注意が必要になります
  • Outlook2024が2029/10/9までサポートされるのでOutlook(Classic)もそれまでは使えるでしょうが、その後は分からないですね

参考

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?