Code:
Option ExplicitSub CATMain()
On Error GoTo 0
Dim oRoot As Document
Set oRoot = CATIA.ActiveDocument
Dim oProduct As Product
Set oProduct = oRoot.Product
Dim oProducts As Products
Set oProducts = oProduct.Products
Dim strOldName As String
Dim strNewName As String
strOldName = oRoot.Name
strNewName = Replace(strOldName, ".CATProduct", "")
oProduct.PartNumber = strNewName
RecursiveThroughTree_fn2pn oProducts
End Sub
Sub RecursiveThroughTree_fn2pn(oProducts As Products)
Dim oProduct As Product
Dim oRefProduct As Product
Dim oRefDocument As Document
For Each oProduct In oProducts
Set oRefProduct = oProduct.ReferenceProduct
Set oRefDocument = oRefProduct.Parent
Dim strOldName As String
Dim strNewName As String
Dim strOldPath As String
Dim strNewFullName As String
If TypeName(oRefDocument) = "ProductDocument" Then
Dim oProdDoc As ProductDocument
Set oProdDoc = oRefDocument
strOldName = oProdDoc.Name
strNewName = Replace(strOldName, ".CATProduct", "")
oProdDoc.Product.PartNumber = strNewName
If oProduct.Products.Count > 0 Then
RecursiveThroughTree_fn2pn2in oProduct.ReferenceProduct.Products
End If
ElseIf TypeName(oRefDocument) = "PartDocument" Then
Dim oPartDoc As PartDocument
Set oPartDoc = oRefDocument
strOldName = oPartDoc.Name
strNewName = Replace(strOldName, ".CATPart", "")
oPartDoc.Product.PartNumber = strNewName
End If
Next
End Sub