vbapowerpointbackwards-compatibilityoffice-2007office-2003

Conditional VBA code for PowerPoint 2003 & 2007/2010/2013 object model compatibility


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?


Solution

  • After developing/debugging in 2007 or later, change this:

    Sub FillShape(oShp as Shape)
    

    to this:

    Sub FillShape(oShp as Object)
    

    Since the compiler doesn't know what properties an Object has or doesn't have, it will no longer bark at you. Of course it's up to you to make sure you don't try to push 2003 through any hoops it doesn't understand or trap the error if you do.