ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
MRMarko Rebec04/11/2016

Hi everyone,

I am dealing with macro to rename parts filename in opened assembly.The name should be the existing name + some custom properties in each part.

I think I am missing something because I get through the assembly and read the component names but can not rename documents.I get un error on RenameDocument witch is consequence of NOT selecting properly with SelectByID2 (I suppose).I am posting the code if anyone can help.

Thank you in advance.

Sub TraverseComponentandRename(swComp As SldWorks.Component2, nLevel As Long)

  

    Dim vChildComp As Variant

    Dim swChildComp As SldWorks.Component2

    Dim swCompConfig As SldWorks.Configuration

    Dim oldName As String

    Dim newName As String

    Dim swSelData As SldWorks.SelectData

    Dim bRet As Boolean

    Dim swConfigMgr As SldWorks.ConfigurationManager

    Dim cusPropMgr  As SldWorks.CustomPropertyManager

    Dim swModelDocExt As SldWorks.ModelDocExtension

  

    Dim ValOut                  As String

    Dim ResolvedValOut          As String

    Dim wasResolved             As Boolean

    Dim lRetVal                 As Long

    Dim status As Boolean

    Dim errorsRename As Long

    Dim sPadStr As String

    Dim i As Long

  

    For i = 0 To nLevel - 1

        sPadStr = sPadStr + "  "

    Next i

  

    vChildComp = swComp.GetChildren

  

    For i = 0 To UBound(vChildComp)

        Set swChildComp = vChildComp(i)

        Set swChildModel = swChildComp.GetModelDoc2

        Set swModelDocExt = swChildModel.Extension

        'swCompConfig = swChildComp.ReferencedConfiguration

      

        Debug.Print sPadStr & swChildComp.Name2

'------------------------------------------------------------------------

        ' Changing component name requires component to be selected

        bRet = swChildComp.Select4(False, swSelData, False)

        oldName = swChildComp.Name2

        'Debug.Print oldName

      

        Set swCompConfig = swChildModel.GetActiveConfiguration

        Set cusPropMgr = swCompConfig.CustomPropertyManager

        lRetVal = cusPropMgr.Get5("Pozicija", False, ValOut, ResolvedValOut, wasResolved)

        Debug.Print "Pozicija:          "; ResolvedValOut; lRetVal

      

        'swChild.Name2 = newName

        status = swModelDocExt.SelectByID2(oldName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

        Debug.Print "Staro ime: " & oldName

        newName = ResolvedValOut & "_" & oldName

        Debug.Print "Novo ime: " & newName

        errorsRename = swModelDocExt.RenameDocument(newName)

        Debug.Print "Rename document errors: " & status; errorsRename

'-----------------------------------------------------------------------------------------------------

        TraverseComponentandRename swChildComp, nLevel + 1

    Next i

End Sub