受欢迎的博客标签

Excel VBA代码封装成Dll(VB6.0): Sub Main in ActiveX DLL projects

Published

If an ActiveX DLL project contains a Sub Main method, the Main method is guaranteed to be executed before any class in the DLL is instantiated. VB6 developers can use this feature to read configuration files, open database connections, and so forth.

Conversely, the Sub Main method is ignored inside a DLL authored in VB.NET, therefore code must be written to ensure that initialization chores be performed before any .NET object is created.

VB Migration Partner ensures that the Sub Main is executed before any class in the DLL is instantiated. This is achieved by adding a static constructor to all public classes in the DLL, as in this code:

Public Class Widget
            Shared Sub New()
                EnsureVB6ComponentInitialization()
            End Sub
            …
        End Class

where the EnsureVB6ComponentInitialization method is a method that invokes the Sub Main method if Widget is the first class being instantiated.
 
 

When an object instance is destroyed, VB6 calls a special parameter-less sub named Class_Terminate. For example, when the variable falls out of scope. Since you cannot specify parameters for this sub, you also cannot overload it.

To explicitly destroy an object, use Set YourClass = nothing.

When an object instance is created from a class, VB6 calls a special sub called Class_Initialize.

https://m.prestwood.com/ASPSuite/KB/CrossRef.asp?LangID=10&ToLangID=11&CatID=&SyntaxID=25

Private Sub Class_Terminate()
    Terminate
End Sub

Public Sub Terminate()
    'Do real termination in here'
End Sub

https://stackoverflow.com/questions/1631321/vb6-collection-remove-doesnt-fire-class-terminate

https://stackoverflow.com/questions/4749381/does-a-vb6-class-have-a-destructor

 

 

 

an ActiveX EXE

 

Option Explicit
Sub Main()
    Dim sPath As String
    Dim ExcelApp As Excel.Application '定义ExcelApp为Excel程序对象
    Dim bCreatApp As Boolean
    Dim wWB As Workbook
   
    sPath = App.Path & "" '获取当前Exe文件所在文件夹
    On Error Resume Next '遇到出错时执行下一语句
    Set ExcelApp = GetObject(, "Excel.Application") '获取已经打开的Excel程序
    bCreatApp = ExcelApp Is Nothing '判断是否获取了Excel程序,如果ExcelApp是Nothing时表示Excel程序没有被运行
    On Error GoTo 0 '恢复出错时提示错误并停止执行功能
    If bCreatApp Then '没有运行Excel程序时
        Set ExcelApp = CreateObject("Excel.Application") '运行Excel程序
        ExcelApp.Visible = True '将Excel程序界面显示出来
    End If
    With ExcelApp '在Excel程序里
        If bCreatApp Then
            Set wWB = .Workbooks.Add '新建一个Excel工作簿
            With wWB '在新建的Excel工作簿内
                With .Sheets(1) '在第一个表内
                    .[A1] = .[A1] + 1
                End With
                .SaveAs sPath & "VB6测试程序(By.Micro).xlsx" '保存工作簿
                .Close '关闭工作簿
            End With
        Else
            Set wWB = .Workbooks.Open(sPath & "VB6测试程序(By.Micro).xlsm") '打开已有的工作簿
            .Run "测试程序" '运行工作簿内已有过程
            wWB.Close True '关闭并保存工作簿
        End If
        If bCreatApp Then .Quit '如果原本没有运行Excel程序时关闭Excel程序
    End With
End Sub

 

In VB 6, a programmer can implement the Class_Terminate event to perform any clean up procedures before an object is destroyed. For instance, if an object held a reference to an open file, it might be important to close the file before destroying the object itself.

https://www.visualbasicplanet.info/visual-basic/finalize-dispose-and-garbage-collection.html

https://www.vbforums.com/showthread.php?172618-Problem-with-Class_Terminate-event

 

Private Sub Class_Initialise()
   AddUserToCollection Me
End Sub

Private Sub Class_Terminate()
   RemoveUserFromCollection Key
End Sub

vba
Dim myUser As clsUser
 
Set myUser = New clsUser
 
'do something with user here...
 
myUser.Destroy
 
Set myUser = Nothing