Archive

Posts Tagged ‘VSTO’

Getting Started with VSTO Programming

April 3, 2011 Leave a comment

Download FullCode Form Here For This Tutorial

Step1
Open New Project in visual studio 2010 Select Projec Type Office and Excel2007 Addin. Give Names to the project and choose the save location

Getting Started With VSTO

Getting Started With VSTO

Step 2
Now the project is created . To create the Ribbon RightClick on ProjectName from SolutionExplorer and click on Add NewItem And Select Ribbon(VisualDesigner) Item in the top pof the list.
Gives a name for the ribbon And click on add button

Getting Started With VSTO

Getting Started With VSTO

Step3
Open ToolBox .you can see different tools on that .From Office Ribbon Controls tab Choose the button control and drag and drop this button control to the Design Of RibbonTab you created on strep2. Change the propertys Of this button like Text,Name etc by clicking on the property’s(Similarly we are doing for Windows Forms Controls)
After Adding buttons the project looks like this

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Step4
Save the Project , build the project and click on the Run Button what you can seen is The Office excel is automatically opened and under Addins Tab you can see the Buttons that you create on step3

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Step5
If you want to change the heading of tab and other stuffs related to this tab Click on the tab and change properties

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

And change the Name ThisAddin in solution Explorer under Excel Logo.

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Step6
Adding a windows form to the solution (For this right click on projects Add new ItemChoose Windows Form).Name the form press OK.(I am adding a new form frmTask1 ).Design this form by adding a label(with text ” Enter Some Text”) , a textbox, and a button
Step7
Double click on one of the buttons you created on the ribbon. Now you got the area for writing code
private void FirstButton_Click(object sender, RibbonControlEventArgs e)
{

}

In the click event Area write this code(Make sure namespace Microsoft.Office.Interop.Excel is added on the top of page.If not include the namespace using code)
using Microsoft.Office.Interop.Excel;

frmTask1 myfrm = new frmTask1();
myfrm.Show();

Save the Project Build it and run the application and Now if you click on the Ribbon Button1 you will get the form that you added like

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#


Get The Selected Row And Column No


Range selectedCell = Globals.HelloAddIn.Application.ActiveCell;
int selectedRow = selectedCell.Row;
int selectedCol = selectedCell.Column;

Pass These 2 values To the form while showing the form

frmTask1 myfrm = new frmTask1(selectedRow,selectedCol);
myfrm.TopMost = true;
myfrm.Show();

And on the form set these 2 values as public variables or properties .So we can use them any time on the form

public partial class frmTask1 : Form
{
public int Selectedrow = 0, SelectedrCol = 0;
public frmTask1(int row,int col)
{
Selectedrow = row;
SelectedrCol = col;
InitializeComponent();
}

}

Step 8
Get the Currently selected workshhet on the windows form

Worksheet activeworksheet = Globals.HelloAddIn.GetActiveWorksheet();

And Add the function GetActiveWorksheet() on to the HelloAddIn.cs page

public Excel.Worksheet GetActiveWorksheet()
{
return (Excel.Worksheet)Application.ActiveSheet;
}

On the form we are entering some text on the textbox and shows that text on the selected cell on the click event of button

private void button1_Click(object sender, EventArgs e)
{
Worksheet activeworksheet = Globals.HelloAddIn.GetActiveWorksheet();
activeworksheet.Cells[Selectedrow, SelectedrCol] = txtTextToShow.Text;
}

Make sure you initialize the values of selected row and selected column while showing the form.
If you add the line
this.Close();

Then the form will also getting closed.

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Getting Started With VSTO, Stepbystep tutorial VSTO Project, VSTO Excel Ribbon using c#

Download FullCode Form Here For This Tutorial