26 Replies Latest reply on Aug 26, 2015 9:44 AM by John Stoltzfus

    Macro to autoname sheets based on Custom Property

    Steve Mlynczak

      I have this code below that works on a single sheet. It grabs the the custom property named "Description" from the inserted model and the sheet size and renames the sheet according to that data The problem is when I add another sheet to the file the macro quits working. Will someone take a look and see what I have messed up. I cant get passed this issue. The problem code I think is highlighted in bold letter below

       

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swDraw As SldWorks.DrawingDoc

      Dim swSheet As SldWorks.Sheet

      Dim swModelview2 As Configuration

      Dim ConfigName As String

      Dim vSheets As Variant

      Dim swView As SldWorks.View

      Dim i As Integer

      Dim Part As SldWorks.ModelDoc2

      Dim Count As String

      Dim SheetProperties As Variant

      Dim paperSize As swDwgPaperSizes_e

      Dim width As Double

      Dim height As Double

      Dim sheetsize As String

      Dim CP As String

      Dim resolvedValOut  As String

      Dim swCustProp      As CustomPropertyManager

       

       

      Sub main()

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

      Set swDraw = swApp.ActiveDoc

      Set swApp = GetObject("", "SldWorks.Application")

      Set Part = swApp.ActiveDoc

       

       

      vSheets = swModel.GetSheetNames

      For i = 1 To swModel.GetSheetCount

      swModel.ActivateSheet vSheets(i - 1)

      Set swSheet = swModel.GetCurrentSheet

      Count = swModel.GetModelViewCount

      Set swView = swModel.GetFirstView

       

       

      '**************************************

       

       

      Set swView = swDraw.GetFirstView

      Set swView = swView.GetNextView

      Set swModel = swView.ReferencedDocument

      Set Sheet = swApp.ActiveDoc.GetCurrentSheet

       

       

      Set swCustProp = swModel.Extension.CustomPropertyManager("")

      swCustProp.Get2 "Description", CP, resolvedValOut

      'MsgBox CP

       

       

      '************ Determine Sheet Size *********************

       

          SheetProperties = swSheet.GetProperties

          paperSize = swSheet.GetSize(width, height)

          If paperSize = 0 Or paperSize = 1 Then

                  sheetsize = "a"

              ElseIf paperSize = 2 Then

                  sheetsize = "b"

              ElseIf paperSize = 3 Then

                  sheetsize = "c"

              ElseIf paperSize = 4 Then

                  sheetsize = "d"

              ElseIf paperSize = 5 Then

                  sheetsize = "e"

              Else

              sheetsize = "d"

          End If

      Do While Not swView Is Nothing

      ConfigName = swView.ReferencedConfiguration

      Set swView = swView.GetNextView

      Loop

      swSheet.SetName i & sheetsize & " - " & CP

      Next i

      End Sub

        • Re: Macro to autoname sheets based on Custom Property
          Deepak Gupta

          Try this

           

          Option Explicit

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swDraw As SldWorks.DrawingDoc

          Dim swSheet As SldWorks.Sheet

          Dim vSheets As Variant

          Dim swView As SldWorks.View

          Dim i As Integer

          Dim SheetProperties As Variant

          Dim paperSize As swDwgPaperSizes_e

          Dim width As Double

          Dim height As Double

          Dim sheetsize As String

          Dim CP As String

          Dim resolvedValOut  As String

          Dim swCustProp      As CustomPropertyManager

           

          Sub main()

          Set swApp = Application.SldWorks

          Set swDraw = swApp.ActiveDoc

           

          vSheets = swDraw.GetSheetNames

          For i = 1 To swDraw.GetSheetCount

          swDraw.ActivateSheet vSheets(i - 1)

           

          Set swSheet = swDraw.GetCurrentSheet

           

          SheetProperties = swSheet.GetProperties

              paperSize = swSheet.GetSize(width, height)

              If paperSize = 0 Or paperSize = 1 Then

                      sheetsize = "a"

                  ElseIf paperSize = 2 Then

                      sheetsize = "b"

                  ElseIf paperSize = 3 Then

                      sheetsize = "c"

                  ElseIf paperSize = 4 Then

                      sheetsize = "d"

                  ElseIf paperSize = 5 Then

                      sheetsize = "e"

                  Else

                  sheetsize = "d"

              End If

            

          Set swView = swDraw.GetFirstView

          Set swView = swView.GetNextView

          Set swModel = swView.ReferencedDocument

          Set swCustProp = swModel.Extension.CustomPropertyManager("")

          swCustProp.Get2 "Description", CP, resolvedValOut

           

           

          swSheet.SetName i & sheetsize & " - " & CP

          Next i

          End Sub

            • Re: Macro to autoname sheets based on Custom Property
              Steve Mlynczak

              Deepak

                That worked great but I ran into another problem that I think is pretty easy. Some of my parts have a "Description" custom setting only in the Configuration Specific tab. When this is the case it doesn't find the Custom Property and leaves it blank. Is there a way to check if the "Description" property is in the default or config specific tab and use that property if it is in one or the other?

                • Re: Macro to autoname sheets based on Custom Property
                  Deepak Gupta

                  Yes but I don't have time as its 1:45 AM for me right now. So probably tomorrow.

                    • Re: Macro to autoname sheets based on Custom Property
                      Steve Mlynczak

                      When you get a chance Deepak. I appreciate your help.

                      • Re: Macro to autoname sheets based on Custom Property
                        Steve Mlynczak

                        I added this if statement but that doesn't seem to do work. If there is no "Description" custom property in custom config tab it errors out and will not go on to the "default" tab to pick up the Description.

                         

                        Set swView = swDraw.GetFirstView
                        Set swView = swView.GetNextView
                        Set swModel = swView.ReferencedDocument
                        Set swCustProp = swModel.Extension.CustomPropertyManager("Default")
                        swCustProp.Get2 "Description", CP, resolvedValOut

                        If CP = "" Then
                            Set swCustProp = swModel.Extension.CustomPropertyManager("")
                            swCustProp.Get2 "Description", CP, resolvedValOut
                            End If


                        swSheet.SetName i & sheetsize & " - " & CP
                        Next i
                        End Sub

                          • Re: Macro to autoname sheets based on Custom Property
                            Steve Mlynczak

                            What I need to do is pull the "Description" custom property from active configuration on the sheet. Will someone show me the code to do this.

                            • Re: Macro to autoname sheets based on Custom Property
                              Adam Hoffman

                              Configuration specific custom property:

                               

                              If CP = "" Then

                                Dim swConfig As SldWorks.Configuration

                                swModel.ShowConfiguration2 swView.ReferencedConfiguration

                                Set swConfig = swModel.GetActiveConfiguration

                                Set swCustProp = swConfig.CustomPropertyManager

                                swCustProp.Get2 "Description", CP, resolvedValOut

                              End if

                                • Re: Macro to autoname sheets based on Custom Property
                                  Steve Mlynczak

                                  Adam-

                                    Thanks, this worked with a few minor tweaks. Here is the final code.

                                  Thanks to all who have helped!!!

                                   

                                  Steve

                                   

                                  Option Explicit
                                  Dim swCustProp As CustomPropertyManager
                                  Dim swApp As SldWorks.SldWorks
                                  Dim swModel As SldWorks.ModelDoc2
                                  Dim swDraw As SldWorks.DrawingDoc
                                  Dim swSheet As SldWorks.Sheet
                                  Dim vSheets As Variant
                                  Dim swView As SldWorks.View
                                  Dim i As Integer
                                  Dim SheetProperties As Variant
                                  Dim paperSize As swDwgPaperSizes_e
                                  Dim width As Double
                                  Dim height As Double
                                  Dim sheetsize As String
                                  Dim CP As String
                                  Dim ITN As String
                                  Dim resolvedValOut  As String

                                   


                                  Sub main()
                                  Set swApp = Application.SldWorks
                                  Set swDraw = swApp.ActiveDoc

                                  vSheets = swDraw.GetSheetNames
                                  For i = 1 To swDraw.GetSheetCount
                                  swDraw.ActivateSheet vSheets(i - 1)

                                  Set swSheet = swDraw.GetCurrentSheet

                                  SheetProperties = swSheet.GetProperties
                                      paperSize = swSheet.GetSize(width, height)
                                      If paperSize = 0 Or paperSize = 1 Then
                                              sheetsize = "a"
                                          ElseIf paperSize = 2 Then
                                              sheetsize = "b"
                                          ElseIf paperSize = 3 Then
                                              sheetsize = "c"
                                          ElseIf paperSize = 4 Then
                                              sheetsize = "d"
                                          ElseIf paperSize = 5 Then
                                              sheetsize = "e"
                                          Else
                                          sheetsize = "d"
                                      End If

                                  Set swView = swDraw.GetFirstView
                                  Set swView = swView.GetNextView
                                  Set swModel = swView.ReferencedDocument
                                  Set swCustProp = swModel.Extension.CustomPropertyManager("")
                                  swCustProp.Get2 "Description", CP, resolvedValOut

                                  If CP = "" Then
                                    Dim swConfig As SldWorks.Configuration
                                    swModel.ShowConfiguration2 swView.ReferencedConfiguration
                                    Set swConfig = swModel.GetActiveConfiguration
                                    Set swCustProp = swConfig.CustomPropertyManager
                                    swCustProp.Get2 "Description", CP, resolvedValOut
                                    swCustProp.Get2 "ItemNumber", ITN, resolvedValOut
                                    Else:
                                    Set swCustProp = swModel.Extension.CustomPropertyManager("Default")
                                    swCustProp.Get2 "ItemNumber", ITN, resolvedValOut
                                   
                                  End If
                                    
                                  swSheet.SetName i & sheetsize & " - " & CP & " #" & ITN
                                  Next i
                                  End Sub

                                    • Re: Macro to autoname sheets based on Custom Property
                                      John Stoltzfus

                                      Thank you Steve -

                                       

                                      I have a question for you

                                       

                                      Here I use the part number/file name to name my sheets which are shown like this:

                                       

                                       

                                      And.... here is your code

                                       

                                      I did try to make a few changes to your macro above to insert the file name rather than the description, could you tell me what I need to change to get what I want??

                                       

                                      Thanks

                                        • Re: Macro to autoname sheets based on Custom Property
                                          Deepak Gupta

                                          Remove # from this line

                                           

                                          swSheet.SetName i & sheetsize & " - " & CP & " #" & ITN

                                           

                                          and it should be like : swSheet.SetName i & sheetsize & " - " & CP & ITN

                                            • Re: Macro to autoname sheets based on Custom Property
                                              John Stoltzfus

                                              I did change that too - swSheet.SetName  sheetsize &  CP & " 00" & i & ITN, which is what I need,  and it works all but I can't figure out where to get the file name or part number code.  - I tried SW-FileName & PartNumber from my custom properties, but it's not working..

                                               

                                              In other words my tabs read as shown above, Part Number-000 .....001, 002 etc

                                              • Re: Macro to autoname sheets based on Custom Property
                                                John Stoltzfus

                                                Deepak -

                                                 

                                                Here is what I changed...

                                                 

                                                vSheets = swDraw.GetSheetNames

                                                For i = 1 To swDraw.GetSheetCount

                                                swDraw.ActivateSheet vSheets(i - 1)

                                                 

                                                 

                                                Set swSheet = swDraw.GetCurrentSheet

                                                 

                                                 

                                                SheetProperties = swSheet.GetProperties

                                                    paperSize = swSheet.GetSize(width, height)

                                                    If paperSize = 0 Or paperSize = 1 Then

                                                            sheetsize = "PartNumber"

                                                        ElseIf paperSize = 2 Then

                                                            sheetsize = "b"

                                                        ElseIf paperSize = 3 Then

                                                            sheetsize = "c"

                                                        ElseIf paperSize = 4 Then

                                                            sheetsize = "d"

                                                        ElseIf paperSize = 5 Then

                                                            sheetsize = "e"

                                                        Else

                                                        sheetsize = "d"

                                                    End If

                                                Set swView = swDraw.GetFirstView

                                                Set swView = swView.GetNextView

                                                Set swModel = swView.ReferencedDocument

                                                Set swCustProp = swModel.Extension.CustomPropertyManager("")

                                                swCustProp.Get2 "PartNumber", CP, resolvedValOut

                                                If CP = "" Then

                                                  Dim swConfig As SldWorks.Configuration

                                                  swModel.ShowConfiguration2 swView.ReferencedConfiguration

                                                  Set swConfig = swModel.GetActiveConfiguration

                                                  Set swCustProp = swConfig.CustomPropertyManager

                                                  swCustProp.Get2 "PartNumber", CP, resolvedValOut

                                                  swCustProp.Get2 "PartNumber", ITN, resolvedValOut

                                                  Else:

                                                  Set swCustProp = swModel.Extension.CustomPropertyManager("Default")

                                                  swCustProp.Get2 "FileName", ITN, resolvedValOut

                                                 

                                                End If

                                                  

                                                swSheet.SetName sheetsize & CP & " 00" & i

                                                 

                                                and this is the result ...

                                                 

                                              • Re: Macro to autoname sheets based on Custom Property
                                                Steve Mlynczak

                                                Try this John.

                                                I added   FN = swModel.GetTitle  -  to get file name.

                                                To get your part number you have to have it set up in your custom properties and then grab it from there.

                                                 

                                                 

                                                Option Explicit
                                                Dim swCustProp As CustomPropertyManager
                                                Dim swApp As SldWorks.SldWorks
                                                Dim swModel As SldWorks.ModelDoc2
                                                Dim swDraw As SldWorks.DrawingDoc
                                                Dim swSheet As SldWorks.Sheet
                                                Dim vSheets As Variant
                                                Dim swView As SldWorks.View
                                                Dim i As Integer
                                                Dim SheetProperties As Variant
                                                Dim paperSize As swDwgPaperSizes_e
                                                Dim width As Double
                                                Dim height As Double
                                                Dim sheetsize As String
                                                Dim CP As String
                                                Dim ITN As String
                                                Dim resolvedValOut  As String
                                                Dim ConfigName As String
                                                Dim FN As String

                                                Sub main()
                                                Set swApp = Application.SldWorks
                                                Set swDraw = swApp.ActiveDoc

                                                vSheets = swDraw.GetSheetNames
                                                For i = 1 To swDraw.GetSheetCount
                                                swDraw.ActivateSheet vSheets(i - 1)

                                                Set swSheet = swDraw.GetCurrentSheet

                                                SheetProperties = swSheet.GetProperties
                                                    paperSize = swSheet.GetSize(width, height)
                                                    If paperSize = 0 Or paperSize = 1 Then
                                                            sheetsize = "a"
                                                        ElseIf paperSize = 2 Then
                                                            sheetsize = "b"
                                                        ElseIf paperSize = 3 Then
                                                            sheetsize = "c"
                                                        ElseIf paperSize = 4 Then
                                                            sheetsize = "d"
                                                        ElseIf paperSize = 5 Then
                                                            sheetsize = "e"
                                                        Else
                                                        sheetsize = "d"
                                                    End If

                                                Set swView = swDraw.GetFirstView
                                                Set swView = swView.GetNextView
                                                Set swModel = swView.ReferencedDocument
                                                Set swCustProp = swModel.Extension.CustomPropertyManager("")
                                                swCustProp.Get2 "Description", CP, resolvedValOut

                                                FN = swModel.GetTitle

                                                If CP = "" Then
                                                  Dim swConfig As SldWorks.Configuration
                                                  swModel.ShowConfiguration2 swView.ReferencedConfiguration
                                                  Set swConfig = swModel.GetActiveConfiguration
                                                  Set swCustProp = swConfig.CustomPropertyManager
                                                  swCustProp.Get2 "Description", CP, resolvedValOut
                                                  swCustProp.Get2 "ItemNumber", ITN, resolvedValOut
                                                  Else:
                                                  Set swCustProp = swModel.Extension.CustomPropertyManager("Default")
                                                  swCustProp.Get2 "ItemNumber", ITN, resolvedValOut
                                                 
                                                End If
                                                    swSheet.SetName FN & " 00" & i
                                                Next i
                                                End Sub

                                      • Re: Macro to autoname sheets based on Custom Property
                                        John Stoltzfus

                                        Steve -

                                         

                                        I can make a few minor changes in a macro - but that's about it, your macro adds a prefix 1a, 2a...... What would I need to change to select another custom property, in other words, my descriptions would be too long, I would need the file name...

                                         

                                        Thanks

                                    • Re: Macro to autoname sheets based on Custom Property
                                      John Stoltzfus

                                      Thanks Steve and Thanks Deepak  - I finally got what works for me, with just a little change to Deepaks code....

                                       

                                      Option Explicit

                                      Dim swApp As SldWorks.SldWorks

                                      Dim swDraw As SldWorks.DrawingDoc

                                      Dim swSheet As SldWorks.Sheet

                                      Dim vSheets As Variant

                                      Dim swView As SldWorks.View

                                      Dim i As Integer

                                      Dim strModelName As String

                                       

                                      Sub main()

                                      Set swApp = Application.SldWorks

                                      Set swDraw = swApp.ActiveDoc

                                       

                                      vSheets = swDraw.GetSheetNames

                                      For i = 1 To swDraw.GetSheetCount

                                      swDraw.ActivateSheet vSheets(i - 1)

                                      Set swSheet = swDraw.GetCurrentSheet

                                      Set swView = swDraw.GetFirstView

                                      Set swView = swView.GetNextView

                                       

                                      strModelName = swView.GetReferencedModelName

                                      strModelName = Mid(strModelName, InStrRev(strModelName, "\") + 1)

                                      strModelName = Left(strModelName, InStrRev(strModelName, ".") - 1)

                                       

                                      swSheet.SetName strModelName

                                       

                                      Next i

                                      End Sub

                                       

                                      **********

                                       

                                      Thanks again - this will save me a lot of time - Here I save out the sheets with a PDF macro and now I don't have to rename my sheets again -