View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Dan[_50_] Dan[_50_] is offline
external usenet poster
 
Posts: 4
Default Passing Objects from Excel VBA collection to a VB6 DLL

Hello Peter,

I did as you said and for some reason this works. I created a new
simplified class for my customer object called CustomerClass. I
created a BuildSQL.dll that has two routines (Startup and
GetCustomerName), the GetCustomerName is passed the object created from
Excel, the Startup is passed an integer. Then I created a VBA user
form in Excel that late binds to the BuildSQL.dll during the form
Initialize event. Two buttons on the form call the procedures. The
BuildSQL.dll contains a reference to the CustomerClass.dll and the
Microsoft Excel workbook contains a reference to the CustomerClass.dll.
The procedures just present a message box of the values passed to
them.

This works! The only thing different between this version and the
previous version that wasn't working is in my customer class. The
Customer Class declared public variables only; "Public Name As String".
The second version uses the LET and GET properties (See below). I
wonder if that made the difference?

Thanks for your suggestions,
Dan

==============================
BuildSQL.DLL

Option Explicit

Sub StartUp(intArg As Integer)

MsgBox "Startup " & CStr(App.EXEName) & " Int:=" & CStr(intArg)

End Sub


Sub GetCustomerName(cCustomer As Customer)

MsgBox cCustomer.Name

End Sub

===============================
CustomerClass.DLL

Option Explicit
Private m_sName As String
Private m_sPhone As String

Public Property Get Name() As String
Name = m_sName
End Property
Public Property Get Phone() As String
Phone = m_sPhone
End Property

Public Property Let Name(ByVal sName As String)
If m_sName < sName Then
m_sName = sName
End If
End Property

Public Property Let Phone(ByVal sPhone As String)
If m_sPhone < sPhone Then
m_sPhone = sPhone
End If
End Property

=================================
Excel Workbook UserForm

Option Explicit

Dim MyPlugIn As Object
Dim MyCustomer As Customer


Private Sub CommandButton1_Click()
MyPlugIn.StartUp 0
End Sub

Private Sub CommandButton2_Click()
Set MyCustomer = New Customer
MyCustomer.Name = "Dan"
MyCustomer.Phone = "333-333-3333"

MyPlugIn.GetCustomerName MyCustomer

Set MyCustomer = Nothing
End Sub

Private Sub UserForm_Initialize()
Dim strDLL As String
Dim progID As String

On Error GoTo Error_Handle
strDLL = "C:\Program Files\Microsoft Visual
Studio\VB98\BuildSQL\BuildSQLProj.dll"

If MsgBox("Did you register the dll file with regsvr32 already?",
vbYesNo) = vbNo Then
Shell "regsvr32 """ & strDLL & """", vbNormalFocus
End If

progID = GetBaseName(strDLL) & ".BuildSQL"
Set MyPlugIn = CreateObject(progID)
Debug.Print progID

Exit Sub
Error_Handle: MsgBox Err.Description
Resume Next
End Sub


Function GetBaseName(path) As String
Dim tmp() As String, ub
tmp = Split(path, "\")
ub = tmp(UBound(tmp))
If InStr(1, ub, ".") 0 Then
GetBaseName = Mid(ub, 1, InStrRev(ub, ".") - 1)
Else
GetBaseName = ub
End If
End Function