1

I am trying to write a single code module that will work with PowerPoint 2003 and all newer versions in relation to the colour model changes that were introduced with 2007 (theme versus scheme in the VBA object model) but this issue could rise with any object model changes.

PowerPoint includes the Application.Version method to check which version of PowerPoint is being used at runtime but it doesn't include an equivalent compiler constant that can be used at compile time with the #If... #Then statements.

In the example below, the second part of the If statement will throw a compiler error in PowerPoint 2003 because the ObjectThemeColor method (and msoThemeColorDark1 constant) doesn't exist in that version of the VBA object model:

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

It's possible to get part of the way to a solution by using the VBA7 compiler constant (which effectively detects PowerPoint 2010 and above) but this leaves 2007 unaccounted for:

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

Is there a way to achieve what I'm trying to do without using the #Const mechanism which would mean maintaining multiple versions of the project?

4

1 回答 1

1

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

Sub FillShape(oShp as Shape)

对此:

Sub FillShape(oShp as Object)

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

于 2013-08-12T15:04:47.363 回答