我正在尝试编写一个代码模块,它将与PowerPoint 2003以及与2007年引入的颜色模型更改有关的所有更新版本( VBA对象模型中的主题与方案)一起工作,但随着任何对象模型的更改,这个问题可能会出现。
PowerPoint包括Application.Version方法来检查运行时使用的是哪个版本的PowerPoint,但是它没有包含一个等效的编译器常量,可以在编译时使用#If.#然后陈述。
在下面的示例中,If语句的第二部分将在PowerPoint 2003中抛出一个编译器错误,因为该版本的ObjectThemeColor对象模型中不存在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机制(这意味着维护项目的多个版本)来实现我所要做的事情的方法?
发布于 2013-08-12 15:04:47
在2007年或更高版本开发/调试之后,更改以下内容:
Sub FillShape(oShp as Shape)对此:
Sub FillShape(oShp as Object)因为编译器不知道对象有或没有什么属性,所以它不会再对您大喊大叫了。当然,这取决于您确保您没有试图推动2003通过任何循环,它不理解或陷阱的错误,如果你这样做。
https://stackoverflow.com/questions/18185755
复制相似问题