Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 7th, 2005, 02:50 PM
Authorized User
 
Join Date: Jun 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Working with AddIns

Hi --

  I would like to create a workbook with generic vba routines and classes available to all other workbooks and their vba code. To do this, I created a new AddIn (TestAddIn.xla) and a test workbook (Test.xls). In the AddIn, I created a module with a function getHello that returns the string "Hello".

If I put:

     = getHello()

in a worksheet cell, it displays "Hello" as expected.

If I create a public sub in a Test.xls module with

       debug.print getHello

and run the sub in the vba editor, nothing happens unless the project for the workbook has a reference to the TestAddIn. Is that correct? Are AddIns visible to vba code only when a reference is created? Does that mean that at the level of a workbook module, it is incorrect to talk of AddIn's and you can only talk of references?


If I create a new class in the AddIn (called AddInClass) with Instancing 1-Private, the class is not visible to the Test.xls vba code - eg the following in a sub fails:

    Set AddIn = New Test_AddIn.AddInClass

where Test_AddIn is the project name of TestAddIn.xla. The following also fail:

    Set AddIn = New TestAddIn.AddInClass 'using root of file name
    Set AddIn = New AddInClass

All 3 get the error message:

    Compile Error: User defined type not defined

If I change the Instancing to 2-PublicNotCreateable and try:

    Set AddIn = New Test_AddIn.AddInClass
    Set AddIn = New AddInClass

they both fail with the message:

    Compile Error: Invalid use of New Keyword

Thus I can use the class, I just can't create it.

If I create an AddIn function to return a new instance of AddInClass:

Public Function getNewAddInClass() As AddInClass
  Set getNewAddInClass = New AddInClass
End Function

then the xls vba code can get a new instance of the AddIn class and use it just fine.

Is there any way to have a workbook module create a new instance of an AddIn class directly without the AddIn needing a function to return the new Instance?

Thanks,

Barry



 
Old February 7th, 2005, 03:24 PM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're right on both counts:

1) you'll need to set a reference in your workbook project to the custom addin you wish to consume

2) VBA can't handle public creatable classes; to overcome this create a function which always returns a new instance of that class (as you've done)





Similar Threads
Thread Thread Starter Forum Replies Last Post
I need a book on C# Addins David_0223 C# 1 May 20th, 2007 09:46 PM
Excel Addins czacharos Excel VBA 1 November 15th, 2005 12:42 PM
Local COM working , but not working at Web Serv nagen111 .NET Web Services 3 February 19th, 2005 04:22 AM
VB6 COM Addins using Excel RefEdit control harnisp1 Excel VBA 0 November 16th, 2004 02:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.