Results 1 to 22 of 22

Thread: Create DLL From Excel Module

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51

    Create DLL From Excel Module

    I am extremely new with VB 6.0, so please bare with me. I have created an Excel project that I would like to create DLLs out of the modules...then access these DLLs within the Excel project. My understanding is that I need to compile these DLLs from within VB. Can someone give me the steps that I need to follow in order to do this? Thanks in advance!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I am afraid that this is not an easy task. You'll need to convert your modules from Excel into class modules of VB code and compile that as a dll. Then you'll need to have Excel reference the dll and create objects from it in order to use it.

    Why do you want to move these modules to a dll?

  3. #3

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    I want to make it so that I can make updates to the dll, send it out to people, and have them replace their existing dll without having to redo all of their worksheets in the Excel project. Do you have a better idea on how to handle this?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I think there is a way to use modules from other Excel Spreadsheets, if so you could have a single spreadsheet with all the module stuff and just redistribute that. Then any other Excel spreadsheets would just refer to it. Kind of how things like Adobe Acrobat incorporate themselves. It's somewhat like how you can reach the default templates modules from any excel spreadsheet. I don't really remember how to do it though, but it should give you another avenue to research.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    That sounds like a good avenue to pursue! However, I think that I am also going to look into the DLL thing, too, just to see how that would be done. Do you have any other insight into the DLL way? Thanks for your help!

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Sure you if you want to convert it to a VB dll there is no problem in that, but you'll most likely have to convert your code to some degree. What kind of things are in the Modules? Just Functions/Subs? Does it handle Excel specific objects (ie Worksheets, Workbooks...)? Have you ever used a class module in VB before? A dll in VB exposes class modules not standard modules so you want be able to cut and paste. Also VB does not have the intrinsic Worksheet and Workbook objects along with others that are specific to Excel's VBA (but you can get to them by referencing the Excel Object in VB).

  7. #7

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    I have done the following, and I am getting a "Can't find DLL entry point..." error.
    I simply started an ActiveX project in VB6, and put the following function in the Class module:

    Public Function Hello() As String
    Hello = "GoodBye"
    End Function

    Then, I've declared this Function inside of Excel and tried to use the Hello function in an Excel module. What exactly am I doing wrong? Thanks.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The method of declaring the functions is for standard dlls (usually made with c/c++ and if you know c/c++ then this is the best route for making the dll) whereas VB makes ActiveX dlls which use COM to expose the entire class not just functions. So you don't do any of that declare stuff you make a reference in your Excel Project to the DLL (references in the project properties area) then you make instances of your class object kind of like this:
    VB Code:
    1. 'if the class was called cMyClass
    2. 'after making a reference
    3. dim Obj as New cMyClass
    4.  
    5. Obj.FunctionNameHere
    6.  
    7. 'then you clean up when you are done
    8. 'to avoid memory leaks
    9. set Obj=nothing

    Or if you are just making a list of functions then change the Instancing property of the class in the dll to MultiUse. That will make it so you don't have to declare an object, but you will still make a reference and then just use the functions as if they were intrinsic to Excel.

  9. #9

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    I understand all of your response except for the last paragraph. What do you mean by "Or if you are just making a list of functions..."? Maybe you could provide an example, like you did for the first part.

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well there really is no code associated with the list. What basically happens when you make the class MultiUse is that it makes all the methods of that class public to the entire project and eliminates the need for declaring an object. It'd be like having With cMyClass at the start of a project and End With at the end (but you don't need the leading .). SO lets say you have the following class named cMyClass and it has functions named MyFunc1 and MyFunc2 then I set the instancing of the class to MultiUse and compile the dll. Then in the Excel project I make a reference to the dll and use the functions as normal:
    VB Code:
    1. 'inside the excel module or project
    2. dim x as long
    3. x=MyFunc1
    4.  
    5. 'or
    6. MyFunc2 Param1

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Opps I just realized that I've been saying to use MultiUse but what I meant was GlobalMultiUse.

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is an example which includes an Excel workbook, a dll, and the code to the dll. You'll have to register the dll or recompile it before using the Excel workbook.

  13. #13

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    By recompiling the dll, does that register the dll?

  14. #14

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    One more thing...is it possible to call functions in one dll from another dll?

  15. #15

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    Along with my last question, can you call a function in one Class in a dll from another function in another Class in the same dll?

    Thanks, and sorry for so many questions :-)

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Ok first yes compiling the dll will register it, also there are rules to whether or not any applications already compiled and/or referencing the dll will continue to work without a recompile or rereference. You can set the compile options to use 'Binary compatibility' then as long as you don't change the existing interface everything will be fine. Which basically boils down to keeping everything backward compatible. For instance you can't change the arguments of an existing function but you can add new functions. Anyway, back to your questions: As for calling functions from other classes and dlls, sure you can. This creates dependancies in your dll but if its needed then its needed. ALso in regards to classes calling other classes, keep in mind that one class can't see the data from another because all that is relative to the instance that you are calling from it. Like if you have ClassA and ClassB and ClassA has a Message property and ClassB as a Show Method. Well if you tried to have ClassA 's Message Show up in ClassB's Method (internally) then that wouldn't work, because the it doesn't know the data of that particular instance of the ClassA object. You could however pass in a reference to the ClassA instance and that would solve the problem. Well I hope that clears things up although some how I think it may make things worse.

  17. #17

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    Ok, I almost got it. Two more questions and that may clear it all up.

    First, regarding the registering of the dll. If I move my project to another computer...one where I didn't compile the dll...I would need to register the dll on that computer, correct? Do I need some sort of software to do this, or is it a manual effort?

    Second, Could you give me an example of what you mean by "You could however pass in a reference to the ClassA instance and that would solve the problem."?

    Thanks for all your help. Like I said earlier, I am fairly new to the VB world.

  18. #18
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can register any control or dlls or what not by excuting one line in the Run dialog of windows, here is an example:
    regsvr32 "C:\WINNT\system32\comdlg32.ocx"

    It's just regsvr32 then the path to the dll in quotes and you'll get a message that it was successful. Also to run a VB dll you'll need the VB Runtimes and any files needed for any of the dependencies of the dll (i.e. any other components that you add). If you want/need to you can make a simple setup package using VB's Package and Development wizard.

    As for passing info into the dll, VB by default passes arguments in by reference so you would just make whatever it is you want passed in a parameter of the method. Example:

    VB Code:
    1. 'lets say that ClassB is a GlobalMultiUse instance dll
    2. 'which means we don't need to make an object to use it
    3. 'and we want to pass the Message property of ClassA
    4. 'which we did make an object of
    5.  
    6. 'assuming the sub is named Show (although this name may conflict other object if it were in an actual GlobalMultiUse dll)
    7.  
    8. dim Obj as new ClassA
    9. Obj.Message="Hey there, pass me over"
    10.  
    11. Show Obj.Message
    12. 'to pass in just the string message like shown here the function in ClassB for Show would look like this:
    13. Public Sub Show(ByVal Msg as string)
    14.  
    15. 'but if we may want other than 1 parameter of the object we can pass the whole thing byref
    16.  
    17. Show Obj
    18. 'to which the sub header would be
    19. Public Sub Show(UseObj as ClassA)
    20.  
    21. 'don't forget to clean up any objects we made
    22. Set Obj=Nothing

  19. #19

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    Cool! That makes sense to me. Thanks for all your help!!!!

  20. #20
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    No problem and welcome to the forums!

  21. #21

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    51
    One more thing before we close this thread...is it possible to "un-register" the dll once I've compiled it? I want to unregister my test dll's that I've compiled and since deleted. Thanks!

  22. #22
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Yes but you'll need to unregister the dlls BEFORE you delete them because it needs the actual file otherwise you'll have to run RegClean (do a search for this on the Microsoft website) or just go through the registry and delete the entries that you don't need anymore. To unregister a dll you use the same method as registering it only with a /u switch added, like this:

    regsvr32 "C:\Program Files\Microsoft Visual Studio\VB98\Temp\dllExample\dllExample.dll" /u <-just add the /u to the end to unregister.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width