Vba 引用的对象不再存在?

Vba 引用的对象不再存在?,vba,coreldraw,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 = FindAllP

因此,我有一个宏来搜索文档上的所有文本,并将它们全部转换为曲线。该宏还将查看超出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
它在某些情况下运行良好,但我在某些文档中发现了一个错误,其中shRange中的每个sh都会生成一个错误“引用的对象不再存在”。显然,这是因为powerclip中有一个嵌套组

我试图通过添加“错误恢复下一步”来忽略错误,宏将正常运行。但是我当然想知道我的代码有什么错误,这样我就可以避免将来的麻烦,我宁愿不忽略宏上的所有错误

下面是一个示例文档来演示错误。


谢谢

我认为遇到的错误是由于FindShapes方法没有返回任何内容

在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

我认为遇到的错误是由于FindShapes方法没有返回任何内容造成的

在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

不,这仍然不起任何作用,错误发生在ForEach循环中,因此一旦进入循环,它将不再检查shRange是否为Nothing。根据shRange中每个sh的语法,不使用CorelDraw测试文件。Shapes?不,这仍然不起任何作用,该错误发生在ForEach循环内部,因此一旦进入循环,它将不再检查shRange是否为Nothing。根据shRange中每个sh的语法,不使用CorelDraw测试文件。Shapes?