4 Replies Latest reply on Apr 11, 2016 12:47 PM by S. P.

    macro to change drawing title and drawing sheets

    S. P.

      Hey guys, this is my first time using forums and macros. I was looking at how to switch multiple sheet names and adjust the title in the sheet format. I got the macro to work for the sheets but I can't get the macro for the title. I want to switch whatever text is in the title to "ABC 10 x i" where i changes on each drawing sheet. The sheet follows the same concept where it is just the latter part "10 x i" and i got that to work using:



      Sub main()

      Dim swApp                   As Object

      Dim swSheet                 As SldWorks.Sheet

      Dim swDraw                  As SldWorks.DrawingDoc

      Dim vSheetNames             As Variant

      Dim i                       As Long


      Set swApp = Application.SldWorks

      Set swDraw = swApp.ActiveDoc

      Set swSheet = swDraw.GetCurrentSheet

      vSheetNames = swDraw.GetSheetNames

          For i = 0 To UBound(vSheetNames)

             swDraw.ActivateSheet (vSheetNames(i))

             Set swSheet = swDraw.GetCurrentSheet

             swSheet.SetName "10 X " & (0 + i * 5)

          Next i

          swDraw.ActivateSheet (vSheetNames(0))

          MsgBox ("Complete")

      End Sub


      This is not my code. I just edited it on a forum I found here. Now I want to use that same for loop configuration and change the title block for each drawing sheet except I'm having trouble getting there. I can understand the coding a little bit but I'm new to these SW classes (or commands), so I don't know how to recall specifically, the title block.


      I was using this code below except I don't want to change other stuff in the sheet format, just the title. If it's easier, I have the description for the part already entered in the property configuration manager and this is exactly what I want in the title. So it would be a string in front of the part description. Sorry I should have clarified this earlier but each drawing sheet is a different part configuration so I would like "ABC (part description)" on each drawing sheet title. I'm going to be away from the desk so I may not respond till 3/4 hours later but I would greatly appreciate any help.

      ' Preconditions: SolidWorks document is open and has a note
      '            containing the text string "abc".
      ' Postconditions: The text string "abc" is replaced with
      '             the text string "def".
      Option Explicit
      Dim swApp   As SldWorks.SldWorks
      Dim swModel As SldWorks.ModelDoc2
      Dim swDraw  As SldWorks.DrawingDoc
      Dim swView  As SldWorks.View
      Dim swNote  As SldWorks.Note
      Dim strNoteText As String
      Const strReplaceTextAs String = "abc"
      Const strNewText    As String = "def"
      Sub main()
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set swDraw = swModel
          Set swView = swDraw.GetFirstView
          Set swNote = swView.GetFirstNote
          While Not swNote Is Nothing
              strNoteText = swNote.GetText
              strNoteText = Replace(strNoteText, strReplaceText, strNewText, 1, -1, vbTextCompare)
              swNote.SetText strNoteText
              Set swNote = swNote.GetNext
      End Sub
        • Re: macro to change drawing title and drawing sheets
          Deepak Gupta

          Do you have the note linked to the model configuration property?

            • Re: macro to change drawing title and drawing sheets
              S. P.

              Thanks for responding Deepak,


              Well about linking the notes,


              1) I wanted to learn how to do macros. I can understand bits and pieces but sometimes more often than not, things just don't work like you want it to. So I would like to understand why that is.


              2) I didn't want to link the notes since I would have to manually add another set of characters to the note for about 150 pages.

                  I wanted to take the part description and add it after a common string of letters. For example lets say my drawing sheets were for printing paper. I would like my note in the title box to say Printing Paper 8.5 X 11. The next sheet would be Printing Paper 8.5 X 14. The Printing paper is the string that I want to add before the "8.5 X ??". This "8.5 X ??" is already entered into the description for each part number. However, I don't need the part description linked to the macro, I can just use a for loop since the 8.5 X ??  adds 3 to the latter number for the next sheet. I thought it might be easier to use custom properties in the macro to recall information. If it's difficult then I'm also content with a for loop. Just trying to learn new things here.


              3) I tried to mix and match between the two codes by selecting the first sheet, change the name, then change the note. However nothing happens. My coding skills are terrible so it's taking me a long time to figure things out. Only the sheet name changes, the notes don't change. Is there a way to select a specific note. I saw other forums where people would do other methods (Editing existing text notes in a drawing ) but they still have to enter the info manually. I can't use the find and replace (2012 SOLIDWORKS API Help - Find and Replace Annotation (VBA) ) since one of the words in the string is also in another note and I can't change both of them. If I get the title note to change by itself then I can use two macros, the other for sheet names.


              4) I can do everything manually but I can save a lot of time instead of having to click edit sheet format, click note, change the letters/order, and so on.

              5) Not related but how do you comment out a section of lines. Let's say I want to comment out 6 successive lines, is there a way or shortcut to do that. BC right now I'm going before each line and inserting '.


              Sorry for bombardment of all replies but I'm just trying to learn here.