Reputation: 44
Coding in VB.net
Custom Usercontrol + Interop - AddIn with Excel
I have been trying to replicate a refedit box for several forms.
(RefEdit: Control to select ranges as in inputbox)
i would use inputbox but is not practical sin just in one form im attemting to use around 15.
I successfuly added the custom control to the form and i tried the behavior of buttons, but when trying to bind it to excel surprise! i get System.NullReferenceException: 'Referencia a objeto no establecida como instancia de un objeto.' Reffering to Active Workbook.
Seems like Excel.Application is not actually working for a usercontrol.
_xlapp = New Excel.Application
Xlwb = _xlapp.ActiveWorkbook 'Fires the Exception
XlAsh = Xlwb.ActiveSheet
I have tried to get object thru marshall
Dim _xlapp=Excel.Application
_xlapp=ctype(GetObject("Excel.Application"),Excel.Application)
Xlwb = _xlapp.ActiveWorkbook
XlAsh = Xlwb.ActiveSheet
And its the same.
I have also tried to deleting the refereces (desperate tried) in the Usercontrol project so it might be bound to the Addin Active Instance.
Im not really sure how the Interop behaves control level.
Any ideas?
Upvotes: 0
Views: 246
Reputation: 1162
You don't need to create a new instance of excel application because you already have one, where your addin is running. You need to use
Dim xlApp As Excel.Application = Globals.ThisAddIn.Application
instead.
This will get an object of application where your addin is loaded and you will be able to get all it's methods and properties (like xlApp.ActiveSheet
or xlApp.ActiveWorkbook
).
Upvotes: 2