Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Afternoon all

I am kind og new to the concept of COM DLL etc and was hoping someone can help me out.

I have created a COM DLL and reference it in VBA. I have mannaged to declare a new class in VBA and then pass values directly to the sub for further calculations.

However, I have a form as part of the DLL and as such I'd like to call the form from say VBA and then fill in values in the form, as soon as form is complete have the values passed to DLL and in return have calculated values passed to VBA for further processing.

Below is relevant code:

COM DLL:
VB
<ComClass(clsCOM_BlockHole.ClassId, clsCOM_BlockHole.InterfaceId, clsCOM_BlockHole.EventsId)> _
Public Class clsCOM_BlockHole

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "f4c2ad73-d1e5-4e33-9365-4de32340604a"
    Public Const InterfaceId As String = "b253ab72-1fef-4547-991b-66e12aa7bfc7"
    Public Const EventsId As String = "abb90279-7ca5-4814-b8d6-cd830eb921c5"
#End Region

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub

    Private dblblockLength As Double
    Private dblblockWidth As Double
    Private dblblockHeight As Double
    Private dblblockVolume As Double
    Private dblblockArea As Double
    Private dblholeDiameter As Double

    Public Sub callForm()
        Dim frmBlock As fromBlock
        frmBlock = New fromBlock
        frmBlock.Show()
    End Sub

    Public Sub calcBlock(ByVal blockHeight As Double, ByVal blockWidth As Double, _
                                ByVal blockLength As Double)

        dblblockHeight = blockHeight
        dblblockLength = blockLength
        dblblockWidth = blockWidth
        dblblockVolume = dblblockHeight * dblblockLength * dblblockWidth
        dblblockArea = 2 * (dblblockHeight * dblblockLength + dblblockLength * dblblockWidth + _
                            dblblockWidth * dblblockHeight)

        If dblblockHeight > dblblockWidth Then
            dblholeDiameter = dblblockWidth * 0.9
        Else
            dblholeDiameter = dblblockHeight * 0.9
        End If

    End Sub

    Public Property holeDiameter() As Double
        Get
            Return dblholeDiameter
        End Get
        Set(ByVal value As Double)
            dblholeDiameter = value
        End Set
    End Property

    Public Property blockLength() As Double
        Get
            Return dblblockLength
        End Get
        Set(ByVal value As Double)
            dblblockLength = value
        End Set
    End Property

    Public Property blockWidth() As Double
        Get
            Return dblblockWidth
        End Get
        Set(ByVal value As Double)
            dblblockWidth = value
        End Set
    End Property

    Public Property blockHeight() As Double
        Get
            Return dblblockHeight
        End Get
        Set(ByVal value As Double)
            dblblockHeight = value
        End Set
    End Property

    Public Property blockVolume() As Double
        Get
            Return dblblockVolume
        End Get
        Set(ByVal value As Double)
            dblblockVolume = value
        End Set
    End Property

    Public Property blockArea() As Double
        Get
            Return dblblockArea
        End Get
        Set(ByVal value As Double)
            dblblockArea = value
        End Set
    End Property

End Class


DLL Form:
VB
Public Class fromBlock
    Inherits System.Windows.Forms.Form

    Private Sub btnBlockOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles btnBlockOK.Click
        Dim useDLLBlockHole As ClassLibrary1.clsCOM_BlockHole
        useDLLBlockHole = New ClassLibrary1.clsCOM_BlockHole
        useDLLBlockHole.calcBlock(txtBlockHeight.Text, txtBlockWidth.Text, txtBlockLength.Text)
        'Me.Close()
    End Sub

    Private Sub btnBlockCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles btnBlockCancel.Click
        Me.Close()
    End Sub
End Class


VBA Code:
VB
Sub useForm()
    Dim useDLLClass As ClassLibrary1.clsCOM_BlockHole
    Set useDLLClass = New ClassLibrary1.clsCOM_BlockHole
    Call useDLLClass.callForm
End Sub
Posted
Comments
JacquesGrobler 12-Aug-12 14:36pm    
Hi Kenneth

Thanks for the post

How would I delcare the sub of the DLL? What goes where?

"[Public | Private] Declare Sub name Lib "libname" [Alias "aliasname"] [([arglist])]"

I don't have a public function to use in VBA and as such don't have need to declare anything.

1 solution

Somethings look a bit strange to me here. Were do you declare the dll's functions? Take a look here:
http://msdn.microsoft.com/en-us/library/office/bb687915.aspx[^]

and this link as well:
http://en.allexperts.com/q/Excel-1059/call-dll-functions-excel.htm[^]
 
Share this answer
 
Comments
enhzflep 12-Aug-12 14:42pm    
(From OP)
Hi Kenneth

Thanks for the post

How would I delcare the sub of the DLL? What goes where?

"[Public | Private] Declare Sub name Lib "libname" [Alias "aliasname"] [([arglist])]"

I don't have a public function to use in VBA and as such don't have need to declare anything.
JacquesGrobler 13-Aug-12 2:11am    
Kenneth

If I reference the DLL in VBA using Tools -> References, surely I dont need to declare it as well?
Kenneth Haugland 13-Aug-12 9:20am    
VBA is not Visual studio, I think you'll have to do it:)
JacquesGrobler 13-Aug-12 9:18am    
Anybody out there for assistance please?
JacquesGrobler 14-Aug-12 2:11am    
Thanks, I will try get it solved with VBA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900