10 Replies Latest reply on Nov 10, 2017 9:07 AM by John Boire

    Adjusting sheet format with a macro

    John Boire

      So here is my issue, I have a macro set up to go through sheets in a drawing and add document numbers to the sheets. (I want to make it difficult to do manually but easy to do when setting up a drawing the first time)


      However when I try to edit the note I run into an issue,
      I select the note as follows:

      ret = swDwg.SelectByID("DetailItem1020@Sheet Format" & i + 1, "NOTE", 0.209024, 0.198179, 0)
      Which works if the sheets are sequential and haven't been rearranged


      However if this makes the document numbers follow the order in which the sheets were added to the drawing. So if I rearrange the sheets  from 1,2,3,4 to 1,3,4,2
      The document numbers  would also get re-ordered


      I tried putting a loop before the main loop that renames the note

      For i = 0 To (SheetNum + (SheetNum / 2))


          If i <= SheetNum - 1 Then

              swDwg.ActivateSheet (SheetName(i))

              ret = swDwg.SelectByID("DetailItem1020@Sheet Format" & i + 1, "NOTE", 0.209024, 0.198179, 0)

                  If ret = True Then

                      Set selObj = selMgr.GetSelectedObject2(1)

                      ret = selObj.SetName("DOC#")

                  End If

          End If


      Next i


      This sets the document name to DOC# , but when I run a loop later to check if there is something called DOC# and to select it, it doesn't work.


      For i = 0 To (SheetNum - 1)


                     boolstatus = swModel.Extension.SelectByID2(SheetName(i), "SHEET", 0.2, 0.05, 0, False, 0, Nothing, 0)

                      swDwg.ActivateSheet (SheetName(i))


                      ret = swDwg.SelectByID("DOC#", "NOTE", 0, 0, 0)

                      Debug.Print (ret)

                          'Check to make sure that an object was selected

                      If ret = True Then

                      'Gets the text note, which should be SldWorks.Note object

                          Set selObj = selMgr.GetSelectedObject2(1)

                      'Call the SldWorks.Note.SetText method to change the text

                          ret = selObj.SetText("<FONT size=11PTS>" & PS(i))

                          swModel.ClearSelection2 True


                     (A bunch of code that adds another 2 Notes)


      I can't figure out why this doesn't work. Is there something I am missing?

      I checked it and the reason is that it still needs the format part
      So new question, is there any way to renumber the format?

        • Re: Adjusting sheet format with a macro
          John Stoltzfus

          John Boire - Take a look at the attache Macro to see what Leon Wurr did when he wrote the macro.. 

            • Re: Adjusting sheet format with a macro
              John Boire

              I am a bit confused as to how this works, It looks like it  just sets up notes for a sheet number



              What I am trying to do is change a prenamed note, I don't know if this method would address my issue of rearranged sheets being numbered in the way they were originally arranged.


              If I can't do that I will do what is being done here, I need to practice.

                • Re: Adjusting sheet format with a macro
                  John Stoltzfus

                  John Boire  I didn't know if there is anything in the code that would help you?

                    • Re: Adjusting sheet format with a macro
                      John Boire

                      The code is interesting but it does something slightly different in that it deletes any note before it and then adds in a new note,
                      I am looking at modifying it to meet my needs, my main issue is that it deletes any note with a specific string mentioned and for my needs I can't exactly do that.
                      The macro relies on the deletion of the previous note in order to do what it needs to do and that is where the main issue is, I need to change an existing note not delete a note and create a new one.

                    • Re: Adjusting sheet format with a macro
                      Deepak Gupta

                      John Boire, could you post some sample files with before and after changes. Then it would be easier to see what exactly you tried to achieve and someone can guide you through.

                        • Re: Adjusting sheet format with a macro
                          John Boire

                          Attached are 2 drawings, one before and one after the macro


                          The after macro shows my problem: The sheets were created in order 000, sheet1, sheet2, sheet3
                          The sheets are then re-arranged to 000, sheet3, sheet1, sheet2
                          The PS# altered by the macro should follow the new order but instead it follows the order that the sheets were created in.

                          I know the problem in my code comes from the "SelectByID" but I don't know an alternative method to select the note, I tried SelectByName but that requires a long input and I don't know what that long refers to.

                            • Re: Adjusting sheet format with a macro
                              Deepak Gupta

                              Why you need a macro to do that. Instead use the sheet number property and then it would always follow the sequential order irrespective of how sheets were added OR even if they are recorded.


                              Check the attached drawing for clarifications.

                              • Re: Adjusting sheet format with a macro
                                Matt Bieringer

                                Here is my suggestion.


                                Make a loop that goes through the notes and looks for the name of the note, in your case it looks like DetailItem1020. This would be done while looping through the sheets. It would look something like this:


                                Sub main()
                                Dim swApp          As SldWorks.SldWorks
                                Dim swModel        As SldWorks.ModelDoc2
                                Dim swDrawModel    As SldWorks.ModelDoc2
                                Dim swDraw          As SldWorks.DrawingDoc
                                Dim swModelView    As SldWorks.ModelView
                                Dim swNote          As SldWorks.Note
                                Dim swSheet        As SldWorks.Sheet
                                Dim i              As Long
                                Dim SheetCount      As Long
                                Dim sheetNames()    As String
                                Dim sheetName      As String
                                Dim currentSheet    As String
                                Dim strNote        As String
                                Dim strNum          As String
                                Set swApp = Application.SldWorks
                                Set swModel = swApp.ActiveDoc
                                Set swDraw = swModel
                                Set swDrawModel = swDraw
                                Set swModelView = swDrawModel.GetFirstModelView
                                Set swSheet = swDraw.GetCurrentSheet
                                currentSheet = swSheet.GetName
                                swModelView.EnableGraphicsUpdate = False  'Turn off Graphic Updates
                                SheetCount = swDraw.GetSheetCount      'Sets total sheet count
                                sheetNames = swDraw.GetSheetNames      'Sets sheet names for array
                                For i = 0 To SheetCount - 1
                                    swDraw.ActivateSheet sheetNames(i)
                                    Set swSheet = swDraw.GetCurrentSheet
                                    Set swView = swDraw.GetFirstView
                                    Set swNote = swView.GetFirstNote
                                    Do While Not swNote Is Nothing
                                        If (swNote.GetName = "DetailItem1020") Then
                                            strNote = swNote.GetText
                                            If strNote = "XXXXX" Then
                                                If i > 10000 Then strNum = CStr(i + 1)
                                                If i < 10000 Then strNum = "0" & CStr(i + 1)
                                                If i < 1000 Then strNum = "00" & CStr(i + 1)
                                                If i < 100 Then strNum = "000" & CStr(i + 1)
                                                If i < 10 Then strNum = "0000" & CStr(i + 1)
                                            End If
                                            swNote.SetText strNum
                                        End If
                                        Set swNote = swNote.GetNext
                                Next i
                                'Returns to current sheet
                                Do Until sheetName = currentSheet
                                    Set swSheet = swDraw.GetCurrentSheet
                                    sheetName = swSheet.GetName
                                    If sheetName <> currentSheet Then swDraw.SheetPrevious
                                swModelView.EnableGraphicsUpdate = True
                                End Sub