Flatten macro problem

I've got a macro that is supposed to flatten a sheet metal part, but every now and then I get a part that it doesn't work on. I'm trying to debug it so I can add in some error handling, but I can't figure out what is causing the problem. Here's the code I'm using, seems to be pretty universal. After the rebuild line, the part has an extra Sheet-Metal# & Flat-Pattern# in the design tree that are unsuppressed, but the Flat-Pattern-1 feature, which contains bends, is still suppressed. Any insight?

Sub Flatten()

    Dim swApp               As SldWorks.SldWorks

    Dim swModel             As SldWorks.ModelDoc2

    Dim nBendState          As Long

    Dim nRetVal             As Long

    Dim bRet                As Boolean

    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

    nBendState = swModel.GetBendState

    Debug.Print "File = " & swModel.GetPathName

    Debug.Print "  BendState    = " & nBendState

    If nBendState <> swSMBendStateFlattened Then

        nRetVal = swModel.SetBendState(swSMBendStateFlattened)

        Debug.Print "  SetBendState = " & nRetVal

        ' Rebuild to see changes

        bRet = swModel.EditRebuild3: Debug.Assert bRet

    End If

End Sub

SolidworksApi macros