15 Replies Latest reply on Sep 12, 2015 8:03 PM by Leon Wurr

    Find and Replace Annotation (VBA) Example Problems

    Taylor Rucker

      Hello everyone,

       

      I have tried to do some digging on this issue but have had no luck. I'm trying to modify an example from the API Help, however the "as-is" copy and paste code does not work. The code stops at " Set swUtil = swApp.GetAddInObject("Utilities.UtilitiesApp") ". The variable swUtil is not defined. I tried adding  " Dim swUtil As Variant " above this line to define the variable. I think variant might not be the most correct but my understanding is variant can have any data type (still new to VBA). Adding the line allowed the macro to continue, but it was hung up on the next line for the same reason. Can someone explain to me why this example macro doesn't work?

       

      Thanks,

       

      Taylor

        • Re: Find and Replace Annotation (VBA) Example Problems
          Deepak Gupta

          You need to make Utilities is selected under Tools > Add-ins before running the macro.

          • Re: Find and Replace Annotation (VBA) Example Problems
            Leon Wurr

            Test this one out:

             

            '---------------------------------------
            '
            ' 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 strReplaceText    As 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
                    swModel.WindowRedraw
                    Set swNote = swNote.GetNext
                Wend
                
            End Sub
            
              • Re: Find and Replace Annotation (VBA) Example Problems
                Taylor Rucker

                You are a wizard Leon!

                 

                Thank you so much for helping me again. I'm trying my best to learn but I hit brick walls quite often. Your macros are helping the learning process though.

                • Re: Find and Replace Annotation (VBA) Example Problems
                  Taylor Rucker

                  Leon,

                   

                  I have been playing with this macro trying to make it traverse all sheets in a drawing. Is there a way to do this? I have looked at other macros that do this but I can't get them to work here.

                    • Re: Find and Replace Annotation (VBA) Example Problems
                      Leon Wurr

                           Updated it with the traverse bit:

                       

                      '---------------------------------------
                      '
                      ' 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 swSheet     As SldWorks.Sheet
                      
                      
                      Dim strNoteText     As String
                      Dim strSheetNames() As String
                      Dim i               As Integer
                      
                      
                      Const strReplaceText    As String = "abc"
                      Const strNewText        As String = "def"
                      
                      
                      Sub main()
                      
                      
                          Set swApp = Application.SldWorks
                          Set swModel = swApp.ActiveDoc
                          Set swDraw = swModel
                          
                          strSheetNames = swDraw.GetSheetNames
                          For i = 0 To UBound(strSheetNames)
                              swDraw.ActivateSheet strSheetNames(i)
                              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
                                  swModel.WindowRedraw
                                  Set swNote = swNote.GetNext
                              Wend
                         Next i
                         
                      End Sub
                      

                       

                       

                          If your drawing happens to have a lot of sheets, I suggest disabling the screen update so the code runs faster:

                       

                           Dim swModelView         As SldWorks.ModelView

                           Set swModelView = swModel.GetFirstModelView 

                           swModelView.EnableGraphicsUpdate = False         'Disable window updating, insert this before the loops

                       

                           swModelView.EnableGraphicsUpdate = True          'Re-enable the screen updating, insert this bit at the end of the macro

                      • Re: Find and Replace Annotation (VBA) Example Problems
                        Deepak Gupta

                        You can add/modify the codes as below

                         

                        Dim vSheetNameArr           As Variant

                        Dim vSheetName              As Variant

                        Dim bRet                    As Boolean

                        Dim i                       As Long

                        vSheetNameArr = swDraw.GetSheetNames

                            For Each vSheetName In vSheetNameArr

                                bRet = swDraw.ActivateSheet(vSheetName)

                                    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 

                                     swModel.WindowRedraw 

                                     Set swNote = swNote.GetNext 

                            Wend

                            Next vSheetName