25 Replies Latest reply on Apr 17, 2017 12:31 PM by Cad Admin

    VBA Replace Text in Note

    Cad Admin

      i have a set of drawings i need to replace part numbers.  Part numbers on drawing are always between Quotes ("").  Existing part numbers on drawings format vary.  I want to find all quoted text on the drawing and replace with new part number.

       

      Examples:

      text in drawing "123456-00" replace with "789456-00"

      text in drawing "12-25165-00" replace with "789456-00"

      text in drawing "QQ-25165D-00" replace with "789456-00"

       

      So basically Find "ANY THING QUOTED" replace with defined new number "789456-00"

       

      I looked at #Task, might work, but I'm not allowed to install it, do to IT restrictions.

        • Re: VBA Replace Text in Note
          Deepak Gupta

          You can use this macro and edit as needed: Change Note Text Example (VBA)

            • Re: VBA Replace Text in Note
              Cad Admin

              Thanks I will take a look at it more...i had seen it but seemed i still would have to "define" what to search for.  Basically I need to replace anything between the quotes...regardless of what it is.

                • Re: VBA Replace Text in Note
                  Deepak Gupta

                  Just used the codes form the same macro and changed to these. This macro will also change notes in the sheet format also.

                   

                  Option Explicit
                  Sub main()
                      Dim swApp           As SldWorks.SldWorks
                      Dim swModel         As SldWorks.ModelDoc
                      Dim swDraw          As SldWorks.DrawingDoc
                      Dim swView          As SldWorks.View
                      Dim swNote          As SldWorks.Note
                  
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      Set swDraw = swModel
                      Set swView = swDraw.GetFirstView
                      
                      While Not swView Is Nothing
                          Set swNote = swView.GetFirstNote
                          While Not swNote Is Nothing
                                If swNote.GetText Like Chr(34) & "*" & Chr(34) Then swNote.SetText Chr(34) & "789456-00" & Chr(34)
                          Set swNote = swNote.GetNext
                          Wend
                          Set swView = swView.GetNextView
                      Wend
                  End Sub
                  
                    • Re: VBA Replace Text in Note
                      Cad Admin

                      Deepak,

                       

                      The code works, but only if the note is the just the quoted text.  If the note contains any other text, it wont change it.

                        • Re: VBA Replace Text in Note
                          Deepak Gupta

                          Cad Admin wrote:

                           

                          So basically Find "ANY THING QUOTED" replace with defined new number "789456-00"

                           

                          Examples:

                          text in drawing "123456-00" replace with "789456-00"

                          text in drawing "12-25165-00" replace with "789456-00"

                          text in drawing "QQ-25165D-00" replace with "789456-00"

                           

                          The code works, but only if the note is the just the quoted text.

                          That was what you asked for

                            • Re: VBA Replace Text in Note
                              Cad Admin

                              Deepak,

                               

                              Guess i should have elaborated...i have a note like:

                               

                                   All parts must be stamped/ etched,

                                   1/4"  & filled with white paint.

                                   Customer P/N: "123456-00"

                                   Qty order: 1200

                               

                              I need to get any quoted string, in this case "123456-00" and replace...

                                • Re: VBA Replace Text in Note
                                  Steven Barry

                                  I already see one potential hurdle to overcome:  your first quotation mark occurs on 1/4"

                                   

                                  You will probably need to think about how you can restrict your search to avoid those kind of conflicts.  For example:  will it only be a continuous string with no spaces?  Or perhaps just filter out anything with a newline in between?  What would happen if you have more than one string like 1/4" in your note?

                        • Re: VBA Replace Text in Note
                          Cad Admin

                          Deepak,

                           

                          Small side question...on the API example Change Note Text Example (VBA) when i run the macro, even if the macro doesn't find anything to change, it changes the formatting of all the notes it reads to what i believe is the document font defaults.

                           

                          So if the original note was 14pt bold times new roman, the macro will change it to 12pt aerial...

                        • Re: VBA Replace Text in Note
                          Christian Chu

                          Cad Admin

                          Part Number is a CusProp while "text on the drawing" is note - so you want to replace note on the dwg with a cusprop (part #)?

                            • Re: VBA Replace Text in Note
                              Cad Admin

                              I currently have or do a find and replace of known strings.

                               

                              What I'm trying to do is scan a drawing...look for ANY quoted text.  The quoted text could be anything.   Then i could replace the text with my new string. 

                               

                              Basically:

                              1.) Find any text between Quotes.  something like FIND "*"....just don't have a wildcard function I'm aware of....

                                   a.) Could be "23456"

                                   b.) Could be "jenaydksf13232",

                                   c.) Could be "The rain in Spain..."

                              2.) Replace with known text.

                                • Re: VBA Replace Text in Note
                                  Christian Chu

                                  Seems to me like you know exactly what to do so what's your question?

                                    • Re: VBA Replace Text in Note
                                      Cad Admin

                                      Function-wise i understand it, but getting the VBA to grab ANY text between quotes is my issue. I've done it if i predefined the "find" string.  But in this case i need to use find ANYTHING with quotes.

                                        • Re: VBA Replace Text in Note
                                          Christian Chu

                                          I see - Here is a code you can use to find string of characters between quote in a variable string which is note on the drawing

                                          then you can use replace function to replace it with "789456-00"

                                          Interesting that Keith Rice

                                          also provides a free macro which you can use - All you need is to sign up to be member

                                           

                                            • Re: VBA Replace Text in Note
                                              Cad Admin

                                              Christian,

                                              I had found that via google as well, I couldn't see any VB that would apply, especially seeing I'm not searching cells, rows, or columns...

                                                • Re: VBA Replace Text in Note
                                                  Christian Chu

                                                  Try this

                                                   

                                                  Dim sNote As String
                                                  Dim sPartNumber As String
                                                  
                                                  
                                                  sNote = "alksdfjlksdfj l" & Chr(34) & "123456-00" & Chr(34) & "ladksjfldasfkj"
                                                  sPartNumber = Mid(sNote, InStr(1, sNote, Chr(34), vbTextCompare) + 2, InStr(InStr(1, sNote, Chr(34), vbTextCompare) + 1, sNote, Chr(34), vbTextCompare) - InStr(1, sNote, Chr(34), vbTextCompare) - 2)
                                                  
                                                  
                                                  MsgBox sPartNumber
                                                  

                                                   

                                                  and here is the one including replace function

                                                   

                                                  Dim sNote As String
                                                  Dim sPartNumber As String
                                                  
                                                  
                                                  sNote = "alksdfjlksdfj l" & Chr(34) & "123456-00" & Chr(34) & "ladksjfldasfkj"
                                                  sPartNumber = Mid(sNote, InStr(1, sNote, Chr(34), vbTextCompare) + 2, InStr(InStr(1, sNote, Chr(34), vbTextCompare) + 1, sNote, Chr(34), vbTextCompare) - InStr(1, sNote, Chr(34), vbTextCompare) - 2)
                                                  
                                                  
                                                  Debug.Print sPartNumber
                                                  Dim sNewNote As String
                                                  sNewNote = Replace(sNote, "123456-00", "789456-00")
                                                  Debug.Print sNewNote
                                                  
                                                    • Re: VBA Replace Text in Note
                                                      Christian Chu
                                                      1. Dim sNote As String 
                                                      2. Dim sPartNumber As String 
                                                      3.  
                                                      4.  
                                                      5. sNote = "alksdfjlksdfj l" & Chr(34) & "123456-00" & Chr(34) & "ladksjfldasfkj" 
                                                      6. sPartNumber = Mid(sNote, InStr(1, sNote, Chr(34), vbTextCompare) + 2, InStr(InStr(1, sNote, Chr(34), vbTextCompare) + 1, sNote, Chr(34), vbTextCompare) - InStr(1, sNote, Chr(34), vbTextCompare) - 2) 
                                                      7.  
                                                      8.  
                                                      9. MsgBox sPartNumber 

                                                      and here is the one including replace function

                                                       

                                                       

                                                       

                                                      1. Dim sNote As String 
                                                      2. Dim sPartNumber As String 
                                                      3.  
                                                      4.  
                                                      5. sNote = "alksdfjlksdfj l" & Chr(34) & "123456-00" & Chr(34) & "ladksjfldasfkj" 
                                                      6. sPartNumber = Mid(sNote, InStr(1, sNote, Chr(34), vbTextCompare) + 2, InStr(InStr(1, sNote, Chr(34), vbTextCompare) + 1, sNote, Chr(34), vbTextCompare) - InStr(1, sNote, Chr(34), vbTextCompare) - 2) 
                                                      7.  
                                                      8.  
                                                      9. Debug.Print sPartNumber 
                                                      10. Dim sNewNote As String 
                                                      11. sNewNote = Replace(sNote, "123456-00", "789456-00") 
                                                      12. Debug.Print sNewNote 
                                                        • Re: VBA Replace Text in Note
                                                          Christian Chu

                                                          Test

                                                          Dim sNote As String
                                                          Dim sPartNumber As String
                                                          
                                                          
                                                          sNote = "alksdfjlksdfj l" & Chr(34) & "123456-00" & Chr(34) & "ladksjfldasfkj"
                                                          sPartNumber = Mid(sNote, InStr(1, sNote, Chr(34), vbTextCompare) + 2, InStr(InStr(1, sNote, Chr(34), vbTextCompare) + 1, sNote, Chr(34), vbTextCompare) - InStr(1, sNote, Chr(34), vbTextCompare) - 2)
                                                          
                                                          
                                                          Debug.Print sPartNumber
                                                          Dim sNewNote As String
                                                          sNewNote = Replace(sNote, "123456-00", "789456-00")
                                                          Debug.Print sNewNote
                                                          
                                                            • Re: VBA Replace Text in Note
                                                              Christian Chu

                                                              Ok here is the good one after clean it up

                                                               

                                                              Dim sNote As String
                                                              Dim sPartNumber As String
                                                              
                                                              
                                                              sNote = "Some notes" & Chr(34) & "123456-00" & Chr(34) & "Some notes"
                                                              sPartNumber = Mid(sNote, InStr(1, sNote, Chr(34), vbTextCompare) + 1, InStr(InStr(1, sNote, Chr(34), vbTextCompare) + 2, sNote, Chr(34), vbTextCompare) - InStr(1, sNote, Chr(34), vbTextCompare) - 1)
                                                              
                                                              
                                                              Debug.Print sPartNumber
                                                              Dim sNewNote As String
                                                              sNewNote = Replace(sNote, sPartNumber, "789456-00")
                                                              Debug.Print sNewNote
                                                              
                                                                • Re: VBA Replace Text in Note
                                                                  Cad Admin

                                                                  Christian,

                                                                   

                                                                  Could work, BUT, i cannot predefine what each note will have (possibilities are endless) in between the quotes...  sort of like needing a wildcard on the

                                                                   

                                                                  Guess i should have elaborated...i have a note like:

                                                                   

                                                                       All parts must be stamped/ etched,

                                                                       1/4"  & filled with white paint.

                                                                       Customer P/N: "123456-00"

                                                                       Qty order: 1200

                                                                   

                                                                  Every drawing is going to have a different quoted number, therefor it cant be predefined...

                                                                  Need to scan notes, and replace only quoted text.