VBA:在Module中首次使用时初始化对象

时间:2014-07-16 19:54:26

标签: vba powerpoint powerpoint-vba

我一直在PowerPoint 2013中使用以下代码接收Object Variable Not Set (Error 91)

我对此做了一步,并在Set SlideCounter = New Counter行标记了这一点,这非常令人困惑。

模块 - 模块1:

Dim SlideCounter As Counter

Sub showNext()
    If SlideCounter Is Nothing Then
        Set SlideCounter = New Counter
    End If
    ActivePresentation.SlideShowWindow.View.GotoSlide (SlideCounter.GetSlideNumber)
End Sub

课程模块 - 计数器:

'You should config the following constants
Const kBeginSlide As Integer = 2
Const kEndSlide As Integer = 4
Const kEnddingSlide As Integer = 5

'You should NOT modify anything below

Dim slides As Collection

Private Sub Class_Initialize()
    Dim x As Integer
    For x = kBeginSlide To kEndSlide
        slides.Add (x)
    Next x
End Sub

Public Function GetSlideNumber()
    If slides.Count = 0 Then
        GetSlideNumber = kEnddingSlide
    Else
        Dim slideIndex As Integer
        slideIndex = GetRandomInteger(1, slides.Count)
        GetSlideNumber = slides.Item(slideIndex)
        slides.Remove (slideIndex)
    End If
End Function

Private Function GetRandomInteger(lowerBound As Integer, upperBound As Integer)
    Randomize
    GetRandomInteger = Int((upperBound - lowerBound + 1) * Rnd + lowerBound)
End Function

0 个答案:

没有答案