Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
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 August 8th, 2005, 06:51 AM
Authorized User
 
Join Date: Feb 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default VB6 DLL linked to Excel workbook

Hi all,

To begin with: I have searched the forums and google for an hour or so to find a good tutorial about creating a Visual Basic 6 DLL, which can be used by Excel VBA..

Here's the problem:

I have created a very simple VB 6 DLL, which contains this code

Public Sub create(vrble)
MsgBox "message generated from VB6 DLL"
MsgBox vrble

Workbooks(vrble).ActiveSheet.Range("A1").Value = "test dll"
End Sub

I did the Project>References>Microsoft Excel 10.0 Library, and also the reference to my DLL in Excel VBA, in where a module contains the following code:

Sub linkdll()
Dim link As clsLinkDLL
Set link = New clsLinkDLL
link.create (ThisWorkbook.Name)
Set link = Nothing
End Sub

''''''''''''''''''''''''''
What I am trying to do, is to run a huge amount of code in an VB6 DLL, but it needs to be linked to my active workbook..
So, for instance, if I have a workbook "Test.xls", and I call a subroutine in the VB6 DLL, it needs to know which workbook is used. So I tried to pass along the name of the workbook, which works initially. The messagebox gives me the name of the workbook in the DLL, but the code after that crashes..

In simple, does anyone know how pass on a workbook, and make sure the DLL can work with that workbook (i.e. put information in cells etc.)

Help would be much appreciated (I know this should be an easy answer, but somehow I couldnt work it out)
 
Old August 8th, 2005, 07:17 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

try passing in the Worksheet object itself e.g.

Code:
Public Sub create(Worksheet As Excel.Worksheet)
  MsgBox "message generated from VB6 DLL"
  MsgBox vrble

  Worksheet.Range("A1").Value = "test dll"
End Sub
and then in your VBA:

Code:
Sub linkdll()
  Dim link As clsLinkDLL
  Set link = New clsLinkDLL
  link.create (ThisWorkbook.ActiveWorksheet)
  Set link = Nothing
End Sub
 
Old August 8th, 2005, 07:28 AM
Authorized User
 
Join Date: Feb 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi gcianfanelli,

Thanks for your fast reply..

I copied your code into my subroutines, but a same kind of error occurs. I would really like to be able to pass along the workbookname, so i have absolute freedom in what I do...

Any other ideas are much appreciated, and if you know any links to an Excel VBA/VB6 DLL tutorial please let me know

Thanks
Max
 
Old August 8th, 2005, 07:59 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem you're having is because your DLL doesn't know about the instance of the Excel application you want to use.

I think you have 2 options:

1. Create a COM Addin for Excel
This will give you direct access to the current instance of the Excel application. You can then correctly identify the Workbook you need.

2. Pass in the Excel application as a parameter in your object's method
This will work just as well as the above, although it will mean you have to explicitly initialise your class or method with the current instance of the Excel application

There are many examples on the web for creating a COM addin, however the 2nd method is probably the best option for you judging by the stage you seem to be at.

Consider modifying your code like so:

Code:
Public Sub create(app as Excel.Application, vrble)
  MsgBox "message generated from VB6 DLL"
  MsgBox vrble

  app.Workbooks(vrble).ActiveSheet.Range("A1").Value = "test dll"
End Sub

Sub linkdll()
  Dim link As clsLinkDLL
  Set link = New clsLinkDLL
  link.create (Application, ThisWorkbook.Name)
  Set link = Nothing
End Sub
 
Old August 8th, 2005, 08:15 AM
Authorized User
 
Join Date: Feb 2005
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi gcianfanelli,

I tried your code, but the statement
link.create (application, ThisWorkbook.Name) gives a syntax error in VBA.

Secondly, allow me to give a little bit more information of my whereabouts..

I already made an addin file (.xla), which is automatically loaded by specific files i created (.xls files). I want to convert this addin into VB6 DLL's (or perhaps, COM Add-In).
I guess it would be best to create a COM addin, as I read above. Would this be of the same security level as of an VB6 DLL? (I guess this will be the same)

However, can this addin contain multiple modules and also (user)forms?

Thanks for the help so far..
Max





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2003 workbook name qualifier AveryB Excel VBA 1 December 17th, 2007 10:41 PM
Import Excel data into a workbook Derek_05 Excel VBA 5 May 31st, 2006 02:02 PM
Link workbook to DLL maxpotters Excel VBA 2 August 9th, 2005 05:26 AM
Closing Excel Workbook rekha_jsr Excel VBA 0 July 9th, 2005 02:40 AM
exporting a workbook from excel to access zisko3 Access 1 February 3rd, 2004 12:05 PM





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