为什么我无法将 PowerPoint 图表分配给 VBA 中的图表变量?
Why am I unable to assign a PowerPoint chart to a chart variable in VBA?
目前,我只是尝试按照说明进行操作 here,但我以前遇到过这个问题。出于某种原因,我无法将 PowerPoint 图表分配给已声明的图表变量。使用 .HasChart,我已经确认那里肯定有一个图表,并且使用 Shapes(i).Type,我已经确认 Shape 是图表类型,但我得到 运行-time error ' 13':每次都输入不匹配。我已经在多个文件和多种图表类型中尝试过此操作,但无济于事 - 我一定遗漏了一些明显而愚蠢的东西,但我无法弄清楚。
例如:
Dim plot As Chart
For Each shp In PPT.Slides(5).Shapes
If shp.HasChart Then
MsgBox (shp.Type)
Set plot = shp.Chart
End If
Next shp
有什么想法吗?
因为 PowerPoint.Chart
与 Excel.Chart
不同(请参阅下面的注释...)。尝试:
Dim plot As PowerPoint.Chart
当你做Dim plot as Chart
时,因为Chart
是Excel
对象模型的成员,VBA将默认使用Excel.Chart
,除非另有说明.
以上假定早期绑定引用了 PowerPoint 库。如果您遇到类似 "User-defined Type not Defined" 的编译错误,这意味着您还没有设置对 PowerPoint 的项目引用。或者,您可以 Dim plot as Object
(通用对象类型),但那样会失去智能感知。
注意:从功能上讲,PowerPoint.Chart
与 Excel.Chart
或 Word.Chart
等本质上相同,它们将具有相同的方法、属性等,并且除了少数例外(none,我很容易想到)你可以对一个人做的任何事情,你都可以对另一个人做。唯一的问题是,当您引用位于 内 PowerPoint 中的图表时,严格来说它不是 Excel.Chart
并且需要限定为 PowerPoint.Chart
(或通用 Object
类型)以避免不匹配错误。
目前,我只是尝试按照说明进行操作 here,但我以前遇到过这个问题。出于某种原因,我无法将 PowerPoint 图表分配给已声明的图表变量。使用 .HasChart,我已经确认那里肯定有一个图表,并且使用 Shapes(i).Type,我已经确认 Shape 是图表类型,但我得到 运行-time error ' 13':每次都输入不匹配。我已经在多个文件和多种图表类型中尝试过此操作,但无济于事 - 我一定遗漏了一些明显而愚蠢的东西,但我无法弄清楚。
例如:
Dim plot As Chart
For Each shp In PPT.Slides(5).Shapes
If shp.HasChart Then
MsgBox (shp.Type)
Set plot = shp.Chart
End If
Next shp
有什么想法吗?
因为 PowerPoint.Chart
与 Excel.Chart
不同(请参阅下面的注释...)。尝试:
Dim plot As PowerPoint.Chart
当你做Dim plot as Chart
时,因为Chart
是Excel
对象模型的成员,VBA将默认使用Excel.Chart
,除非另有说明.
以上假定早期绑定引用了 PowerPoint 库。如果您遇到类似 "User-defined Type not Defined" 的编译错误,这意味着您还没有设置对 PowerPoint 的项目引用。或者,您可以 Dim plot as Object
(通用对象类型),但那样会失去智能感知。
注意:从功能上讲,PowerPoint.Chart
与 Excel.Chart
或 Word.Chart
等本质上相同,它们将具有相同的方法、属性等,并且除了少数例外(none,我很容易想到)你可以对一个人做的任何事情,你都可以对另一个人做。唯一的问题是,当您引用位于 内 PowerPoint 中的图表时,严格来说它不是 Excel.Chart
并且需要限定为 PowerPoint.Chart
(或通用 Object
类型)以避免不匹配错误。