VSTO Add-in

Introduced in Excel 2007.
VSTO Add-ins must implement the IStartup interface.
More information on how to call code inside a VSTO Add-in from VBA can be found here.
More information on how to add Excel User Defined Functions to a VSTO Add-in can be found here.


Creating

Open Visual Studio 2022 as administrator.
New Project, C#, Windows, Office, Excel VSTO Add-in for Excel 2013 and newer versions.
Change the Name to "ExcelVSTOAddin".
Change the Location to somewhere on your C drive.
Check the .NET Framework version is correct and press OK.
The solution contains one project and that project contains a single source file called ThisAddIn.cs
Add the following MessageBox's to the Startup and Shutdown events.

namespace ExcelVSTOAddin 
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("fires when add-in is loaded");
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("fires when add-in is unloaded");
        }

        private void InternalStartup()
        {
             this.Startup += new System.EventHandler(ThisAddIn_Startup);
             this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
    }
}

Build the Solution

Select (Build > Build Solution).
This will add the Registry Entry required to load the add-in into Excel.
When you clean the solution the registry key will be automatically removed.
When you use this template you do not have to set the Properties, Debug tab, "start external program".
When you use this template you do not have to tick the Properties, Build tab, "Register for COM Interop".
If you want to debug your office add-in just add a break point before loading.


Load the VSTO Add-in

Select (Debug > Start Debugging).
Excel will open and the add-in will be automatically loaded.
You will see the following message appear when Excel is opened.

microsoft excel docs

You will see the following message appear when you close Excel.

microsoft excel docs

Properties

You can display the project properties by double clicking on the Properties folder.

microsoft excel docs

AssemblyInfo.cs - This file contains important attributes that are used to describe this assembly (title, description, version, comvisible, guid, etc)
Resources.resx - This file contains information from the Properties, Resources tab and should not be manually edited.
Settings.settings - This file contains information from the Properties, Settings tab and should not be manually edited.


References

In this folder you will see a list of all the .NET assembly references that are used within this project.
Some of these are general .NET framework libraries (such as System.Windows.Forms)
Others are specific VSTO libraries that would only be used when creating a VSTO solution.
All the following references will be added to the project automatically
This list depends on the specific version of Visual Studio and the version of Office and the .NET framework you are targeting.

Accessibility - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\Accessibility.dll
Microsoft.CSharp - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\Microsoft.CSharp.dll
Microsoft.Office.Interop.Excel - (15.0.0)C:\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\
Office15\Microsoft.Office.Interop.Excel.dll
Microsoft.Office.Tools - (10.0.0)C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\
ReferenceAssemblies\v4.0\Microsoft.Office.Tools.dll
Microsoft.Office.Tools.Common - (10.0.0)C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\
ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Common.dll
Microsoft.Office.Tools.Common.v4.0.Utilities - (10.0.0)C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\
ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Common.v4.0.Utilities.dll
Microsoft.Office.Tools.Excel - (10.0.0)C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\
ReferenceAssemblies\v4.0\Microsoft.Office.Tools.Excel.dll
Microsoft.Office.Tools.v4.0.Framework - (10.0.0)C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\
ReferenceAssemblies\v4.0\Microsoft.Office.Tools.v4.0.Framework.dll
Microsoft.VisualStudio.Tools.Application.Runtime - (10.0.0)C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\
ReferenceAssemblies\v4.0\Microsoft.VisualStudio.Tools.Applications.Runtime.dll
Office - (15.0.0)C:\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\
Office15\Office.dll
stdole - (7.0.3300)C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\stdole.dll
System - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\System.dll
System.Core - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\System.Core.dll
System.Data - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\System.Data.dll
System.Data.DataSetExtensions - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\System.Data.DataSetExtensions.dll
System.Drawing - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\System.Drawing.dll
System.Windows.Forms - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\System.Windows.Forms.dll
System.Xml - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\System.Xml.dll
System.Xml.Linq - (4.0.0)C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\
.NETFramework\v4.8\System.Xml.Linq.dll

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext