我几天前才开始使用VBA。我注意到一些命令似乎在我的计算机上不起作用,我想知道这是否是由于我的计算机设置造成的。
我在 Windows 7 上的 PowerPoint 2013 中使用 VBA,通过 MacOSX 上的 VMware Fusion(虚拟机)运行。我需要创建对活动幻灯片的动态引用,但是这样做的几种方法破坏了我的代码:
Set oSl = Application.ActiveWindow.View.Slide
(这里建议)
Set oSl = ActivePresentation.Slides(ActiveWindow.View.Slide.SlideNumber)
(这里建议)
Set oSl = ActiveWindow.Selection.SlideRange.SlideIndex
(这里建议)
这些都不适合我。由于我刚开始使用 VBA,我只是在代码的不同部分之后插入消息框,并查看不再触发的框 - 在这种情况下,总是在我用上面描述的各种其他方法替换的“oSl =”行之后. 此外,
Set oSl = ActiveWindow.Selection.SlideRange(1)
还破坏了我的代码(如此处所述)
到目前为止所做的工作是
Set oSl = ActivePresentation.SlideS(1)
上面所有不起作用(但应该)的方法都包含“ActiveWindow”。如果您能告知我选择活动幻灯片的方法是否存在错误,或者问题是否可能是由于我的 PowerPoint 在虚拟机上运行而导致 VBA 无法正确访问“ActiveWindow”,那就太好了。如果是这种情况,是否有另一种方法可以在不使用 ActiveWindow 的情况下选择当前活动的幻灯片?
编辑:我正在尝试将此应用于 PowerPoint 中的以下代码。基本上我想要做的是将“oSl = ActivePresentation.SlideS(1)”行替换为一行代码,该代码行并不总是针对幻灯片 1,而是当前处于活动状态的幻灯片。我的问题不是如何做到这一点——网上有很多关于如何做到这一点的说明。我的问题是为什么这些方法对我不起作用。
Sub SelectionMacro()
Dim oSl As Slide
Dim oSh As Shape
Dim aArrayOfShapes() As Variant
Dim ShapeX As Shape
Dim N As Long
Dim Temp As Variant
Dim J As Long
Dim FadeEffect As Effect
Set oSl = ActivePresentation.SlideS(1)
'This section creates an array of all pictures on Slide1 called
'"aArrayOfShapes"
For Each oSh In oSl.Shapes
If oSh.Type = msoPicture Then
On Error Resume Next
Debug.Print UBound(aArrayOfShapes)
If Err.Number = 0 Then
ReDim Preserve aArrayOfShapes(1 To UBound(aArrayOfShapes) + 1)
Else
ReDim Preserve aArrayOfShapes(1 To 1)
End If
Set aArrayOfShapes(UBound(aArrayOfShapes)) = oSh
End If
Next
'This section creates a random index number within the bounds of the
'length of aArrayOfShapes and assigns the shape with that index number
'to the Shape object ShapeX
Randomize
NumberX = Int((UBound(aArrayOfShapes) - (LBound(aArrayOfShapes) - 1)) * Rnd) + LBound(aArrayOfShapes)
Set ShapeX = aArrayOfShapes(NumberX)
'This section shuffles aArrayOfShapes
For N = LBound(aArrayOfShapes) To UBound(aArrayOfShapes)
J = CLng(((UBound(aArrayOfShapes) - N) * Rnd) + N)
If N <> J Then
Set Temp = aArrayOfShapes(N)
Set aArrayOfShapes(N) = aArrayOfShapes(J)
Set aArrayOfShapes(J) = Temp
End If
Next N
'This section loops through all Shapes in aArrayOfShapes and
'fades them out one by one EXCEPT for ShapeX
For Each Shape In aArrayOfShapes
If ShapeX.Name <> Shape.Name Then
Set FadeEffect = oSl.TimeLine.MainSequence.AddEffect _
(Shape:=Shape, effectid:=msoAnimEffectFade, trigger:=msoAnimTriggerAfterPrevious)
With FadeEffect
.Timing.Duration = 0.5
.Exit = msoTrue
End With
End If
Next Shape
End Sub