このウォークスルーでは、Microsoft Office Excelのアプリケーションレベルのアドインを作成する方法を紹介します。このソリューションで作成される機能は、開いているワークブックに関係なく、アプリケーション自体で利用可能です。また、Office Add-insモデルの利用を検討することも推奨します。VSTOアドインプロジェクトを作成し、ワークブック保存時にテキストを追加するコードを書く手順を示しています。最後に、開発環境からアドインをクリーンアップする方法も説明されます。
Microsoft Excelのアプリケーションレベルのアドインを作成する方法
この入門ガイドでは、Microsoft Office Excel用のアプリケーションレベルのアドインを作成する方法を説明します。この種類のソリューションで作成した機能は、開いているワークブックに関係なく、アプリケーション自体で利用可能です。
対象
このトピックの情報は、Excel用のVSTOアドインプロジェクトに適用されます。詳細については、Officeアプリケーションおよびプロジェクトタイプごとの利用可能な機能を参照してください。
複数のプラットフォームでOffice体験を拡張するソリューションを開発に興味がありますか?新しい[Officeアドインモデル](https://learn.microsoft.com/en-us/office/dev/add-ins/)をチェックしてください。OfficeアドインはVSTOアドインやソリューションと比べて小さなフットプリントを持っており、HTML5、JavaScript、CSS3、XMLなど、ほぼすべてのWebプログラミング技術を使用して構築できます。
このガイドで行うタスク
- Excel VSTOアドインプロジェクトを作成する。
- Excelのオブジェクトモデルを使用して、ワークブックが保存されるときにテキストを追加するコードを記述する。
- プロジェクトをビルドして実行し、テストする。
- 完了したプロジェクトを整理し、VSTOアドインが開発コンピュータで自動的に実行されないようにする。
以下の手順でVisual Studioのユーザーインターフェース要素の名前や場所が異なる場合があります。これらの要素は、使用しているVisual Studioのエディションと設定によって異なります。詳細については、[IDEのパーソナライズ](https://learn.microsoft.com/en-us/visualstudio/ide/personalizing-the-visual-studio-ide?view=vs-2022)を参照してください。
必要条件
このガイドを完了するには、以下のコンポーネントが必要です。
プロジェクトの作成
Visual Studioで新しいExcel VSTOアドインプロジェクトを作成する手順
- Visual Studioを起動します。
- [ファイル]メニューで、[新規作成]をポイントし、[プロジェクト]をクリックします。
- テンプレートペインで、[Visual C#]または[Visual Basic]を展開し、[Office/SharePoint]を展開します。
- 展開された[Office/SharePoint]ノードの下で、[Officeアドイン]ノードを選択します。
- プロジェクトテンプレートのリストから、[Excel 2010アドイン]または[Excel 2013アドイン]を選択します。
- [名前]ボックスに、[FirstExcelAddIn]と入力します。
- [OK]をクリックします。
Visual StudioはFirstExcelAddIn
プロジェクトを作成し、エディタにThisAddIn
コードファイルを開きます。
保存されたワークブックにテキストを追加するコードを書く
次に、ThisAddIn
コードファイルにコードを追加します。この新しいコードはExcelのオブジェクトモデルを使用して、アクティブなワークシートの最初の行にヒントとなるテキストを挿入します。アクティブなワークシートは、ユーザーがワークブックを保存する際に開いているワークシートです。デフォルトでは、ThisAddIn
コードファイルには、次の生成されたコードが含まれています。
-
ThisAddIn
クラスの部分定義。これは、コードのエントリーポイントを提供し、Excelのオブジェクトモデルへのアクセスを提供します。 ThisAddIn_Startup
およびThisAddIn_Shutdown
イベントハンドラー。これらのイベントハンドラーは、ExcelがVSTOアドインを読み込むときとアンロードするときに呼び出されます。
保存されたワークブックにテキストの行を追加する手順
ThisAddIn
コードファイルで、次のコードをThisAddIn
クラスに追加します。
void Application_WorkbookBeforeSave(Microsoft.Office.Interop.Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
Excel.Range firstRow = activeWorksheet.get_Range("A1");
firstRow.EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
Excel.Range newFirstRow = activeWorksheet.get_Range("A1");
newFirstRow.Value2 = "This text was added by using code";
}
- C#を使用している場合は、
ThisAddIn_Startup
イベントハンドラーに次の必要なコードを追加します。
this.Application.WorkbookBeforeSave += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
プロジェクトのテスト
プロジェクトをテストする手順
F5
を押してプロジェクトをビルドし、実行します。- Excelでワークブックを保存します。
- ワークブックに以下のテキストが追加されていることを確認します。
- This text was added by using code.
- Excelを閉じます。
プロジェクトの整理
プロジェクトの開発が完了したら、VSTOアドインのアセンブリ、レジストリエントリ、およびセキュリティ設定を開発コンピュータから削除します。そうしないと、開発コンピュータでExcelを開くたびにVSTOアドインが実行され続けます。
開発コンピュータで完了したプロジェクトを整理する手順
- Visual Studioで、[ビルド]メニューをクリックし、[ソリューションのクリーン]を選択します。
次のステップ
基本的なVSTOアドインをExcel用に作成したので、これらのトピックからVSTOアドインを開発する方法についてさらに学ぶことができます。
関連コンテンツ
—(※関連コンテンツについては、各自で調査してください)
————-
Create Visual Studio Tools for Office Add-ins: Microsoft Excel – Visual Studio (Windows)
Source link
This instructional guide explains how to create an application-level VSTO (Visual Studio Tools for Office) Add-in for Microsoft Excel. Such Add-ins extend features to the Excel application itself, regardless of the open workbooks.
Key Points:
-
Target Audience: The guide is intended for developers working with VSTO Add-in projects for Excel.
- Add-ins Overview: There’s a mention of the newer Office Add-ins model, which is lightweight and can be built using web technologies like HTML, JavaScript, CSS, and XML.
Walkthrough Tasks:
-
Create an Excel VSTO Add-in Project:
- Open Visual Studio.
- Create a new project under Office/SharePoint templates, selecting either Excel 2010 or Excel 2013 Add-in.
- Name the project (e.g., "FirstExcelAddIn").
-
Write Code:
- Insert code into the ThisAddIn class to add boilerplate text to the first row of the active worksheet when it is saved.
- Handle the WorkbookBeforeSave event to execute the insertion of text.
-
Test the Project:
- Build and run the project.
- Save a workbook in Excel to check if the text ("This text was added by using code") is added as intended.
- Clean Up:
- Remove the VSTO Add-in and associated settings from your development environment to prevent it from running automatically in Excel.
Additional Notes:
- Prerequisites: Ensure the needed components and Visual Studio are set up before starting the walkthrough.
- IDE Variations: Visual Studio’s interface may differ based on the version, so users should adjust based on their environment.
Next Steps:
After completing this basic Add-in, developers are encouraged to explore more advanced topics related to VSTO Add-in development.