How to call a VBA routine in an add-in from a VB app?

  • Thread starter Thread starter Camden
  • Start date Start date
C

Camden

I have an Excel XLA add-in, written in VBA: call it "MyAddin.xla". Say
it has a module "AModule" and a Public Sub called "SomeRoutine()". I
want to be able to write an app in VB that will start up Excel,
activate the add-in, and then call SomeRoutine().

I've been able to do the first couple parts of that: I can call up
Excel from a VB app, and then step through the AddIns collection, find
my add-in, and make sure .Installed = True. All well and good. I can
add a workbook, then a worksheet, then change the name of the
worksheet, no problems.

Problem is, I haven't a clue as to how to call SomeRoutine() from my
VB app. I have an Object, addinMine, that I set equal to the Addins
item named "MyAddin.xla". After that I've tried:

- addinMine.Commands.SomeRoutine
- addinMine.SomeRoutine
- appExcel.Run addinMine.FullName & "!SomeRoutine"
- appExcel.Run addinMine.FullName & "!Commands.SomeRoutine"

Yes, those last two only work for workbook macros, but I still had to
try.


I've dug around in the docs I have, and dug around using Google, but
no luck. Can any of you folks tell me how to do what I want to do?
Failing that, can anyone tell me why it cannot be done? Thanks for any
help you can offer.
 
When you start excel through automation, addins are not loaded. You can
load your addin with this code posted by KeepItCool;

code from KeepItCool (Oct 21, 2004)
Sub LoadXLwithAddins()
Dim xl As Object
Dim ai As Object

Set xl = CreateObject("Excel.Application")

For Each ai In Application.AddIns
If ai.Installed Then
xl.Workbooks.Open(ai.FullName).RunAutoMacros 1
End If
Next

xl.Visible = True
Set xl = Nothing
End Sub

Once the addin in loaded, then you should be able to run a macro in it by
using the RUN command.

. . . Run "MyAddin.xla!SomeRoutine"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top