新建一个ThingDemo.DLL工程 第一步:建一个模块Module1内容为:Option Explicit Public gdatServerStarted As Date Sub Main() '部件开始时要执行的代码, ' 对第一个对象的请求作出反应。 gdatServerStarted = Now Debug.Print "E xecuting Sub Main" End Sub'为对象提供唯一标识符的函数。 Public Function GetDebugID() As Long Static lngDebugID As Long lngDebugID = lngDebugID + 1 GetDebugID = lngDebugID End Function 第二步:建一个类模块Thing内容为:Option Explicit Public Name As String Private mlngDebugID As LongPublic Property Get DebugID() As Long DebugID = mlngDebugID End Property Public Sub ReverseName() Dim intCt As Integer Dim strNew As String For intCt = 1 To Len(Name) strNew = Mid$(Name, intCt, 1) & strNew Next Name = strNew End SubPrivate Sub Class_Initialize() '获得由只读的 DebugID 属性 ' 返回的调试 ID。 mlngDebugID = GetDebugID Debug.Print "Initialize Thing " & DebugID _ & ", Name=" & Name End SubPrivate Sub Class_Terminate() On Error Resume Next Debug.Print "Terminate Thing " & DebugID _ & ", Name=" & Name End Sub 第三步:建一个窗体Form1内容为:Option Explicit '对 Thing 对象的引用。 Private mthTest As Thing'按钮“Create New Thing”。 Private Sub Command1_Click() On Error GoTo errorhandler Set mthTest = New Thing mthTest.Name = InputBox("Enter a name for the Thing", "Test Thing") Exit Sub errorhandler: MsgBox "找不到文件ThingDemo.dll" End Sub '按钮“Show the Thing”。 Private Sub Command2_Click() MsgBox "Name: " & mthTest.Name, , "Thing " & mthTest.DebugID End Sub'按钮“Reverse the Thing's Name”。 Private Sub Command3_Click() mthTest.ReverseName '通过设置值来单击“Show the Thing”。 Command2.Value = True End Sub'按钮“Release the Thing”。 Private Sub Command4_Click() Set mthTest = Nothing End Sub先编译成DLL,这样你可以引用ThingDemo就可以了,我已测试过可以的,再有问题看MSDN帮助
如果想要一般DLL,那好象没办法....
Public gdatServerStarted As Date
Sub Main()
'部件开始时要执行的代码,
' 对第一个对象的请求作出反应。
gdatServerStarted = Now
Debug.Print "E xecuting Sub Main"
End Sub'为对象提供唯一标识符的函数。
Public Function GetDebugID() As Long
Static lngDebugID As Long
lngDebugID = lngDebugID + 1
GetDebugID = lngDebugID
End Function 第二步:建一个类模块Thing内容为:Option Explicit
Public Name As String
Private mlngDebugID As LongPublic Property Get DebugID() As Long
DebugID = mlngDebugID
End Property
Public Sub ReverseName()
Dim intCt As Integer
Dim strNew As String
For intCt = 1 To Len(Name)
strNew = Mid$(Name, intCt, 1) & strNew
Next
Name = strNew
End SubPrivate Sub Class_Initialize()
'获得由只读的 DebugID 属性
' 返回的调试 ID。
mlngDebugID = GetDebugID
Debug.Print "Initialize Thing " & DebugID _
& ", Name=" & Name
End SubPrivate Sub Class_Terminate()
On Error Resume Next
Debug.Print "Terminate Thing " & DebugID _
& ", Name=" & Name
End Sub 第三步:建一个窗体Form1内容为:Option Explicit
'对 Thing 对象的引用。
Private mthTest As Thing'按钮“Create New Thing”。
Private Sub Command1_Click()
On Error GoTo errorhandler
Set mthTest = New Thing
mthTest.Name = InputBox("Enter a name for the Thing", "Test Thing")
Exit Sub
errorhandler:
MsgBox "找不到文件ThingDemo.dll"
End Sub
'按钮“Show the Thing”。
Private Sub Command2_Click()
MsgBox "Name: " & mthTest.Name, , "Thing " & mthTest.DebugID
End Sub'按钮“Reverse the Thing's Name”。
Private Sub Command3_Click()
mthTest.ReverseName
'通过设置值来单击“Show the Thing”。
Command2.Value = True
End Sub'按钮“Release the Thing”。
Private Sub Command4_Click()
Set mthTest = Nothing
End Sub先编译成DLL,这样你可以引用ThingDemo就可以了,我已测试过可以的,再有问题看MSDN帮助