COM DLL表格VBA [英] COM DLL Form VBA
问题描述
下午全部
我对COM DLL等的概念很陌生,希望有人可以帮助我.
我创建了一个COM DLL,并在VBA中引用了它.我不得不在VBA中声明一个新类,然后将值直接传递给子类以进行进一步的计算.
但是,我有一个窗体作为DLL的一部分,因此我想从VBA中调用该窗体,然后在窗体中填写值,一旦窗体完成,就将值传递给DLL并返回已将计算值传递给VBA进行进一步处理.
以下是相关代码:
COM DLL:
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:
<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形式:
DLL Form:
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代码:
VBA Code:
Sub useForm()
Dim useDLLClass As ClassLibrary1.clsCOM_BlockHole
Set useDLLClass = New ClassLibrary1.clsCOM_BlockHole
Call useDLLClass.callForm
End Sub
推荐答案
在这里让我有些奇怪.您是否声明了dll的功能?在这里看看:
http://msdn.microsoft.com/en-us/library/office/bb687915.aspx [^ ]
以及此链接:
http://en.allexperts.com/q/Excel-1059/call- dll-functions-excel.htm [ ^ ]
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[^]
这篇关于COM DLL表格VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!