VBA Word 从 normal.dotm 创建 class 模块对象

VBA Word Create class module object from normal.dotm

我用 VBA (Word) 的函数和子程序建立了自己的库,并想创建一个新的 class 模块。但我不知道如何将这个新 class 与其他文件一起使用。

秒表示例(Normal.dotm、Class 模块:Global_StopWatch)

Private mlngStart As Long
Private Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long

Public Sub StartTimer()
    mlngStart = GetTickCount
End Sub
Public Function EndTimer() As Long    
    EndTimer = (GetTickCount - mlngStart)
End Function

秒表调用(Document.dotm,模块:测试)

Sub swTest()
    Dim gSW As Global_StopWatch
    Set gSW = New Global_StopWatch
        
    gSW.StartTimer
    Debug.Print "That took " & gSW.EndTimer & " ms."
End Sub

有人可以帮忙吗?

Normal.dotm - clsStopWatch(实例化 = PublicNotCreatable)

Option Explicit

Private mlngStart As Long
Private Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long

Public Sub StartTimer()
    mlngStart = GetTickCount
End Sub
Public Function EndTimer() As Long
    EndTimer = (GetTickCount - mlngStart)
End Function

Normal.dotm - 模块 1

Public Function StopWatch() As clsStopWatch
    Set StopWatch = New clsStopWatch
End Function

文档 1 - 模块 1

Sub Tester()
    Dim sw As normal.clsStopWatch, i As Long
    Set sw = normal.stopwatch
    sw.StartTimer
    For i = 1 To 10000000#
        '
    Next i
    Debug.Print sw.endtimer
End Sub