【发布时间】:2013-08-13 16:35:58
【问题描述】:
我正在尝试编写一个可以与 PowerPoint 2003 以及与 2007 年引入的颜色模型更改相关的所有较新版本的单个代码模块(VBA 对象模型中的主题与方案),但这个问题可能会随着任何对象模型更改。
PowerPoint 包含 Application.Version 方法来检查在运行时使用的 PowerPoint 版本,但它不包含可在编译时与 #If...#Then 语句一起使用的等效编译器常量。
在下面的示例中,If 语句的第二部分将在 PowerPoint 2003 中引发编译器错误,因为该版本的 VBA 对象模型中不存在 ObjectThemeColor 方法(和 msoThemeColorDark1 常量):
Option Explicit
Public Enum PPTversion
PPT2003 = 11
PPT2007 = 12
PPT2010 = 14
PPT2013 = 15
End Enum
Sub FillShape(oShp as Shape)
If Int(Application.Version) = 11 Then
' Use the old colour model
oShp.Fill.ForeColor.SchemeColor = ppForeground
Else
' Use the new colour model
' causes a compiler error "Method or data member not found" when run in 2003
oShp.Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1
End If
End Sub
可以通过使用 VBA7 编译器常量(有效地检测 PowerPoint 2010 及更高版本)获得解决方案的一部分,但这会使 2007 年下落不明:
Option Explicit
Public Enum PPTversion
PPT2003 = 11
PPT2007 = 12
PPT2010 = 14
PPT2013 = 15
End Enum
Sub FillShape(oShp as Shape)
If Int(Application.Version) = 11 Then
' Use the old colour model
oShp.Fill.ForeColor.SchemeColor = ppForeground
Else
' Use the new colour model
#If VBA7 Then
oShp.Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1
#End If
End If
End Sub
有没有办法在不使用 #Const 机制的情况下实现我想要做的事情,这意味着维护项目的多个版本?
【问题讨论】:
标签: vba powerpoint backwards-compatibility office-2007 office-2003