Skip to main content
1-Visitor
March 22, 2017
Question

Can I use component path leaf/root pointers in Excel VBA?

  • March 22, 2017
  • 1 reply
  • 1303 views

Running up against what might be a bug in the VB API when using Excel.

This is simply trying to get the PTC-provided example listSubAssemblyComponent to function in Excel VBA rather than its original VB. I intended to make this the basis of code to extract the model tree into an Excel spreadsheet for internal engineering management purposes - old way we do this is to save the model tree from Creo, and import it into a spreadsheet via VBA macros, but I want a more streamlined way to do it. (This ain't necessarily it, and I'm open to better alternatives.)

It appears to allocate something to currentPath (typing ?currentPath is Nothing in the immediate window returns false). But it hangs up every time on the line with currentPath.Leaf, returning the message:


Run-time error '-2147352567 (80020009)':

pfcExceptions::XToolkitGeneral Error


I don't see any useful explanation of this error. I've fiddled around with everything I can think up on how to get around it, but I'm starting to believe it's a bug or else something available in VB but not VBA for some reason.

Ideas? What am I missing?


Sub listSubAssemblyComponent(ByVal currentLevel As Cintseq)

'======================================================================

'Function  :  listEachLeafComponent

'Purpose    :  This function This method is used to recursively visit

'              all levels of the assembly structure.

'======================================================================

    Dim currentComponent As IpfcSolid

    Dim currentPath As IpfcComponentPath

    Dim level As Integer

    Dim subComponents As IpfcFeatures

    Dim i, id As Integer

    Dim componentFeat As IpfcFeature

    level = currentLevel.Count

    Set currentPath = Nothing

    Dim dummyModel As IpfcModel

    '======================================================================

    'Special case, level is 0 for the top level assembly.

    '======================================================================

    If (level > 0) Then

        Set muhAssy1 = New CMpfcAssembly ' *** original PTC VB code had this step inline with currentPath, but that won't work with Excel VBA

        Set currentPath = muhAssy1.CreateComponentPath(asm, currentLevel) ' *** was originally = (New CMpfcAssembly).CreateComponentPath(asm, currentLevel)

        Set currentComponent = currentPath.Leaf ' *** this is where the failure occurs ***

    Else

        Set currentComponent = asm

    End If

    Set dummyModel = currentComponent ' *** as above, added this step because in Excel VBA, currentComponent's type (IpfcSolid) doesn't allow the .Type property)

    If (dummyModel.Type = EpfcModelType.EpfcMDL_PART) And (level > 0) Then

    ' *** code below is original PTC example, just changed from VB to VBA syntax in Excel ***

        pathArray.Add currentPath                        

    Else

        '==================================================================

        'Find all component features in the current component object.

        'Visit each (adjusting the component id paths accordingly).

        '==================================================================

        Set subComponents = currentComponent.ListFeaturesByType(True, EpfcFeatureType.EpfcFEATTYPE_COMPONENT)

        For i = 0 To subComponents.Count - 1

            Set componentFeat = subComponents.item(i)

            id = componentFeat.Number

            currentLevel.Set level, id

            listSubAssemblyComponent currentLevel

        Next

    End If

    '======================================================================

    'Clean up current level of component ids before returning up one level.

    '======================================================================

    If Not level = 0 Then

        currentLevel.Remove level - 1, level

    End If

    Return

End Sub

1 reply

12-Amethyst
June 13, 2026

'======================================================================
'This function is used to recursively visits all levels of the assembly structure.
'======================================================================
Private Function listSubAsmComponents(ByVal currentLevel As Cintseq)
    Dim currentComponent As IpfcSolid
    Dim currentComponentModel As IpfcModel
    Dim currentPath As IpfcComponentPath
    Dim componentFeat As IpfcModelItem
    Dim subComponents As IpfcFeatures
    Dim Compids As New Cintseq
    Dim CMpfcAssembly_ As New CMpfcAssembly
    Dim i, id, level As Integer
    
    
        level = currentLevel.Count
        PrintInfo (Str(level))
        
                '======================================================================
                'Special case, level is 0 for the top level assembly.
                '======================================================================
            If level > 0 Then
                Set currentPath = CMpfcAssembly_.CreateComponentPath(useAsm, currentLevel)
                Set currentComponent = currentPath.Leaf
                Set currentComponentModel = currentPath.Leaf
                
                Else
                    
                    Set currentComponent = useAsm
                    Set currentComponentModel = useAsm
                    
            End If
              
            If (currentComponentModel.Type = EpfcMDL_PART) And (level > 0) Then
                    pathArray.Add currentPath
                Else
                
                ' //** REMOVE below lines if Not interested in collecting Assemblies
                If Not currentPath Is Nothing Then
                    pathArray.Add currentPath
                End If
                ' //** REMOVE above lines if Not interested in collecting Assemblies
                
                    '==================================================================
                    'Find all component features in the current component object. Visit each (adjusting the component id paths accordingly).
                    '==================================================================
                    
                    Set subComponents = currentComponent.ListFeaturesByType(False, EpfcFeatureType.EpfcFEATTYPE_COMPONENT)
                        
                        For i = 0 To (subComponents.Count - 1)
                            If (subComponents.Item(i).Status = EpfcFeatureStatus.EpfcFEAT_ACTIVE) Then 'Collect only Active Components
                                Set componentFeat = subComponents.Item(i)
                                id = componentFeat.id
                                currentLevel.Set level, id
                                Call listSubAsmComponents(currentLevel)  '#### RECURSION
                            End If
                        Next i
            End If
            '======================================================================
            'Clean up current level of component ids before returning up one level.
            '======================================================================
            If Not level = 0 Then
            currentLevel.Remove level - 1, level
            End If
      
End Function