Lin Kejian

Hi:

I have create a project using "Shared Add-in", and success create a menu item in mainmenu ,

the menu can response the event like :

private void Button_Click(Microsoft.Office.Core.CommandBarButton ctrl, ref bool CancelDefault)
{
try
{
MessageBox.Show("this is event of Run");

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

}

What I want is make the event access excel workbook , like add a NamedRange,

What can I do step by step

by the way , sorry for my pool English!




Re: Visual Studio Tools for Office How to access Excel workbook,sheet etc. in an Shared Add-ins

X4U

Hello Linkejian,

has nothing todo with VSTO - but you can play around like this:

Excel.Application _ExcelApplication;

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)

{

_ExcelApplication = application as Excel.Application ;

addInInstance = addInInst;

}

private void Button_Click(Microsoft.Office.Core.CommandBarButton ctrl, ref bool CancelDefault)

{

try

{

Excel.Workbook activeWorkbook = _ExcelApplication.ActiveWorkbook;

Excel.Worksheet sheet = activeWorkbook.ActiveSheet as Excel.Worksheet;

Excel.Range range = sheet.Cells["1", "B"] as Excel.Range;

range.Value2 = "Test";

}

catch (Exception ex)

{

//MessageBox.Show(ex.ToString());

}

}

hope this helps,

greets, Helmut