Reputation: 23
OK, so I've done a lot of googling trying to find information on this topic and I've come up pretty much empty handed. Maybe I'm not searching for the correct terminology for what I'm trying to accomplish.
My issue is that I've written a function in a MS Excel add-in, I followed the instructions from Microsoft as a starting point, but their tutorial has the code execute every time the user saves the document. My goal is to have a button on the ribbon I designed execute this function rather than the save button.
This is the Microsoft article that I followed to get myself started: https://msdn.microsoft.com/en-us/library/cc668205.aspx
I also found this question on here, but it didn't have enough detail for me to figure out how to implement the solution for myself: How to connect a ribbon button to a function defined in an Excel add-in?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
namespace ExcelAddIn1
{
public partial class ThisAddIn
{
void FormatTime(Microsoft.Office.Interop.Excel.Workbook WB, bool SaveAsUi, ref bool Cancel)
{
/////MY FUNCTION BODY HERE//////
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Thanks in advance for your assistance.
Upvotes: 2
Views: 3227
Reputation: 49397
VSTO provides two ways for creating a custom UI:
The Ribbon designer - see Walkthrough: Creating a Custom Tab by Using the Ribbon Designer.
A raw XML markup - Walkthrough: Creating a Custom Tab by Using Ribbon XML.
In both cases you may access the add-in properties and methods using the Globals.ThisAddin
property which returns an instance of the add-in class (shown in your code listed above).
Upvotes: 1
Reputation: 1474
Usually you can use Globals.ThisAddIn.Application to access application level and document level UI. I hope this link can help. Here is a sample of adding a button to a worksheet like this:
Globals.Factory.GetVstoObject(
Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[1])
.Controls.AddControl(button, selection, buttonName);
Looks like
Upvotes: 0