This project has moved. For the latest updates, please go here.

How to access the workbook object from COMAddin

Dec 16, 2013 at 7:26 AM
Dear members, I'm new to Visual Studio . I've programming background in vb and vba only and I'm new to NetOffice.

I've added a sample procedure to the NetOffice 1.6.0\NET 4.0\Examples\Excel\VB\COMAddin Examples

Public Sub Test()
Dim excelApplication As New Excel.Application
Dim workBook As Excel.Workbook = excelApplication.ActiveWorkbook
Dim workSheet As Excel.Worksheet = workBook.ActiveSheet
workSheet.Cells(1, 1).Value = "This is a sample text"
End Sub

When this procedure has been called from Ribbon Control I'm getting the error 'Object reference not set to an instance of an object'. I'm unable to identify the error. Your help is highly appreciated.
Developer
Dec 16, 2013 at 3:35 PM
Edited Dec 16, 2013 at 3:40 PM
For project COMAddinRibbonExample from NetOffice 1.6.0\NET 4.0\Examples\Excel\VB\COMAddin Examples your code should look like this:
Public Sub Test()
  Dim workBook As Excel.Workbook = _excelApplication.ActiveWorkbook
  Dim workSheet As Excel.Worksheet = workBook.ActiveSheet
  workSheet.Cells(1, 1).Value = "This is a sample text"
End Sub
_excelApplication is already declared in Addin class and initialized in OnConnection sub. Test sub shoud be Addin class member.
Dec 16, 2013 at 4:04 PM
Dear friend, my sincere thanks for this piece of advice. I was trying different things for the past 2 days and I'm unable to figure out what went wrong and nor did I noticed this OnConnection sub. Once again thank you very much.