vba - 无法迭代 CustomerData 集合(无效过程调用错误)

标签 vba excel powerpoint

我正在尝试在 PowerPoint 演示文稿的图表形状内存储 xml 元数据。

我需要确保形状仅包含当前的xml数据,因此我有一个函数来删除现有数据(如果有)并添加当前数据。

Sub Test()
Dim cht as Chart
Dim xml as String
Set cht = ActivePresentation.Slides(1).Shapes(1)
xml = "<Chart property1='true' property2='false'>blah blah blah</Chart>"

    EmbedChartXML xml, cht

End Sub

Sub EmbedChartXML(xml As String, cht As Shape)
Dim x As Variant

    'Get rid of any previous data
    For Each x In cht.CustomerData
        x.Delete
    Next

    Set xmlPart = cht.CustomerData.Add

    xmlPart.LoadXML xml
End Sub

For Each 循环失败,并显示指示的错误消息。我可以看到 cht.CustomerData.Count = 2 (例如),但如果我尝试在“本地”窗口中查看此内容,则会收到相同的错误。

enter image description here

更新

这是另一个失败的测试例程,即使 For/Next 循环内没有任何内容。

Sub TestIteration()
Dim sld As Slide
Dim pres As Presentation
Dim shp As Shape
Dim x As CustomXMLPart

Set pres = ActivePresentation
Set sld = pres.Slides(2)

For Each shp In sld.Shapes

    Set pptCustomerData = shp.CustomerData

    For Each x In shp.CustomerData

'    For Each pptCustomXMLPart In pptCustomerData
'        Debug.Print pptCustomXMLPart.Id
'    Next
    Next
Next


End Sub

最佳答案

这里的其他人无法在 Excel 2013 中复制此错误(我使用的是 Excel 2010),其中集合对象本身变得无法访问。也许存在错误或其他问题,但我在 Google 上找不到太多有关类似错误的信息...

我最终得出了这个解决方案。我不确定为什么 CustomXMLPart 的 .Delete 方法实际上并没有从集合中删除该项目(而是将其变为Nothing,并且然后连续迭代尝试失败)。

相反,我使用 CustomerData.Delete 方法。这似乎按预期工作。

Sub EmbedChartXML(xml As String, cht As Shape)
Dim x As Variant
Dim cData As CustomerData

Dim xID As String

    Set cData = cht.CustomerData
    Debug.Print cht.CustomerData.Count

    'Get rid of any previous data
    For Each x In cData
        xID = x.Id
        cData.Delete xID
    Next

    With cData.Add
        .LoadXML xml
    End With

    Debug.Print cht.CustomerData.Count
End Sub

关于vba - 无法迭代 CustomerData 集合(无效过程调用错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23160275/

相关文章:

excel - 将月份名称转换为数字

excel - 拖动时更新公式行和列

excel - 如何在3D公式中勾勒出多个列和选项卡的范围?

vba - 如何使用 VBA "ungroup"PowerPoint 中的图像

powerpoint - 在 Linux 中将 Powerpoint 转换为 Flash

vba - VB中的ElseIf语句

excel - 在 Excel 散点图中使用文本作为水平标签

excel - 将 Excel 文件读入 Access 数据库的最佳方法

VBA - 在长公式上使用 Application.Evaluate 时出错

math - 如何在 PowerPoint VBA 中求平方根?