How to access methods in ThisAddin.cs file

I created an Excel Addin project in C #. The solution now contains the ThisAddin.cs file, which has the ThisAddin class. Later I added an element called Form to the same solution. In the form, when I click on the button, for this button, click on the event that I want to call the method inside the ThisAddin.cs file.

namespace ExcelAddIn { public partial class ThisAddIn { public void RefreshExcelData() { } } } 

Now in MyForm.cs, when trying to create an object for the ThisAddin class, there is a compilation error in which the Thisaddin class does not have a constructor that takes 0 arguments.

 private void btnUploadTestCases_Click(object sender, EventArgs e) { ThisAddIn objrefresh = new ThisAddin(); } 

What am I missing here?

+4
source share
2 answers

You are approaching a problem from the wrong direction. When you click the button, you don’t want to create a new add-in, what you really need is to access the add-in instance that VSTO creates for you when you start Excel, which is accessible through Globals.ThisAddIn ,

Change your code in the Form to the following:

 private void btnUploadTestCases_Click(object sender, EventArgs e) { var addIn = Globals.ThisAddIn; addIn.RefreshExcelData(); } 

... and he must work in charm.

As the saying goes, is there a good reason for this method to be on ThisAddIn? In general, ThisAddIn should be used to connect and reset the add-in when Excel starts / shuts down, and I would recommend putting in as little logic as possible.

+10
source

Use this code:

Globals.ThisAddIn.Application.StatusBar = "Referesh clicked!!!."; Globals.ThisAddIn.RefreshExcelData();

Just make sure your function remains public .

+3
source

All Articles