引用的对象不再存在?

时间:2017-04-18 04:51:40

标签: vba coreldraw

所以我有一个宏来搜索文档上的所有文本并将它们全部转换为曲线。这个宏也会查看超出CQL范围的powerclip。

以下是我的代码:

Public Sub convertText()
    Dim pg As Page
    Dim shRange As ShapeRange
    Dim sh As Shape

    For Each pg In ActiveDocument.Pages
        pg.Activate
        Set shRange = FindAllPCShapes.Shapes.FindShapes(Query:="@type='text:artistic' or @type='text:paragraph'")
        For Each sh In shRange
            sh.ConvertToCurves
        Next sh
    Next pg
End Sub

Function FindAllPCShapes(Optional LngLevel As Long) As ShapeRange ' Shelby's function
    Dim s As Shape
    Dim srPowerClipped As New ShapeRange, srJustClipped As New ShapeRange
    Dim sr As ShapeRange, srAll As New ShapeRange
    Dim bFound As Boolean, i&

    bFound = False
    If ActiveSelection.Shapes.count > 0 Then
        Set sr = ActiveSelection.Shapes.FindShapes()
    Else
        Set sr = ActivePage.Shapes.FindShapes()
    End If
    i = 0
    Do
        For Each s In sr.Shapes.FindShapes(Query:="!@com.powerclip.IsNull")
            srPowerClipped.AddRange s.PowerClip.Shapes.FindShapes()
        Next s
        If srPowerClipped.count > 0 Then bFound = True: i = i + 1
        If i = LngLevel And bFound Then Set FindAllPCShapes = srPowerClipped: Exit Function
        bFound = False
        srAll.AddRange sr
        sr.RemoveAll
        sr.AddRange srPowerClipped
        If LngLevel = -1 Then srJustClipped.AddRange srPowerClipped
        srPowerClipped.RemoveAll
    Loop Until sr.count = 0

    If LngLevel = -1 Then
        Set FindAllPCShapes = srJustClipped
    Else
        Set FindAllPCShapes = srAll
    End If
End Function

在某些情况下它可以正常工作,但我在某些文档中发现错误,其中For Each sh In shRange将生成错误“引用的对象不再存在”。显然这是因为powerclip中的嵌套组。

我尝试通过添加On Error Resume Next忽略错误,宏将运行良好。但当然我想知道我的代码有什么错误,所以我可以避免将来的麻烦,我宁愿不要忽略我的宏上的所有错误。

这是一个演示错误的示例文档。 https://www.dropbox.com/s/lpi568eoltc8cxy/ReferenceError.cdr?dl=0

谢谢

1 个答案:

答案 0 :(得分:0)

我认为遇到的错误是由于FindShapes方法返回 Nothing

在For循环之前,您应该检查它是否 Nothing

For Each pg In ActiveDocument.Pages
    pg.Activate
    Set shRange = FindAllPCShapes.Shapes.FindShapes(Query:="@type='text:artistic' or @type='text:paragraph'")
    If Not shRange Is Nothing Then
        For Each sh In shRange
            sh.ConvertToCurves
        Next sh
    End If
Next pg