【问题标题】:Conditional VBA code for PowerPoint 2003 & 2007/2010/2013 object model compatibility用于 PowerPoint 2003 和 2007/2010/2013 对象模型兼容性的条件 VBA 代码
【发布时间】: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


    【解决方案1】:

    在 2007 年或以后开发/调试后,更改此:

    Sub FillShape(oShp as Shape)
    

    到这里:

    Sub FillShape(oShp as Object)
    

    由于编译器不知道对象具有或不具有哪些属性,因此它不会再对您咆哮。当然,您要确保不要试图将 2003 推入任何它不理解的圈套,或者如果您这样做,就会陷入错误。

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      相关资源
      最近更新 更多