Calling methods in a VSTO project

245 views
Skip to first unread message

Simon

unread,
Jun 28, 2011, 5:29:43 AM6/28/11
to Excel-DNA
Hi,

Is there a simple way of calling methods defined in a VSTO project
from ribbon code defined in a .dna file? In the past I haved used VBA
to initiate the calls for me but I would now like to move away from
relying on VBA.

Simon.

Govert van Drimmelen

unread,
Jun 28, 2011, 7:27:55 AM6/28/11
to Excel-DNA
Hi Simon,

That sound a bit tricky.
How did you call the VSTO methods from VBA? The same technique should
work from Excel-DNA.

Is there a way to call the methods in the VSTO add-in as macros, say
from a button on a worksheet? (I don't think so.)

Is it the extended object model that VSTO provides that keeps
your .NET code there, instead of moving it to a regular assembly you
expose through Excel-DNA?

-Govert

Simon

unread,
Jun 28, 2011, 10:00:03 AM6/28/11
to Excel-DNA
Hi Govert,

Yes, it is the extended methods that keeps the code in VSTO. We are
using VSTO to host visual controls on a worksheet - now if you could
do that with DNA, I'd be interested.

I have since found a way to call into VSTO, and you're right again,
with the same code used in VBA but with late bound VB.NET. I was
hoping there was a simpler way. Code below...

Dim addIn As ICOMAddin
Dim addInFunctions As Object = Nothing

Try
addIn =
Insight.Configuration.ExcelProviders.Application.COMAddins.Item("MyAddin")
If Not addIn Is Nothing Then
addIn.Connect = True
If addIn.Connect Then
addInFunctions = addIn.Object
End If
End If

If Not addInFunctions Is Nothing Then
addInFunctions.DoSomething()
End If

Catch ex As COMException
Throw New ApplicationException("Unable to connect to
Visualisation COM Addin.", ex.InnerException)
Catch ex As Exception
Throw New ApplicationException("Unable to activate
Visualisation COM Addin.", ex.InnerException)
End Try

Thanks again,

Simon.
> > Simon.- Hide quoted text -
>
> - Show quoted text -

Govert van Drimmelen

unread,
Jun 28, 2011, 10:33:53 AM6/28/11
to Excel-DNA
Hi Simon,

Thanks for writing back. Yes - that makes sense through AddIn.Object.

If you are using VSTO you are already picking a Primary Interop
Assembly and making sure that it is installed. So you might as well
reference it from your Excel-DNA add-in, so no need to access the
object model late-bound.

I'm not sure what's behind your
Insight.Configuration.ExcelProviders.Application property, but from
your Excel-DNA add-in you should probably get the Application object
via ExcelDnaUtil.Application, else you might be getting the wrong
instance of Excel from the ROT (you might get the last instance
started, instead of the instance hosting you add-in).

I don't know enough about VSTO to know about the controls on Worksheet
story. Is this surfaced as =EMBED(...) in Excel? If so, I guess a
helper around a Windows Forms UserControl might be enough.

Cheers,
Govert

Simon

unread,
Jun 28, 2011, 1:22:32 PM6/28/11
to Excel-DNA
Hi,

Yes - Insight.Configuration.ExcelProviders.Application is just a
wrapper for ExcelDnaUtil.Application.

And...yes the worksheet controls are surfaced in VSTO as
=EMBED("WinForms.Control.Host.V3","")

Simon.
> > > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages