众所周知,在Excel VBA中有ThisWorkbook,在Word VBA中有ThisDocument,但是不知道为啥PowerPoint VBA并不提供ThisPresentation,对于加载宏工具来说,经常需要引用工具所在的Presentation对象,既然微软官方没有提供这个对象,那么咱们自己动手丰衣足食。
示例代码如下。
Function ThisPresentation() As Presentation
Dim p As Presentation
For Each p In Presentations
If p.Name = "MyTools.ppam" Then
Set ThisPresentation = p
Exit Function
End If
Next
End Function
代码解析:
第3~8行代码使用For语句循环遍历所有Presentation对象。
第4行代码判断Presentation对象的Name属性是否为指定值,如果满足条件,第5行代码将对象引用作为返回值,第6行代码结束程序执行。
使用起来也非常简单:
Sub demo()
Dim objPre As Presentation
Set objPre = ThisPresentation
MsgBox objPre.Name
End Sub