DDonnachie Posted August 8, 2006 Share Posted August 8, 2006 I am using AutoCAD 2002, and Office 2000. I am trying to use VB to lift values from an Excel sheet and overwrite values in an autocad drawing. I have managed to get VB to create text objects in the drawing, but what I would prefer to do is use VB to replace each piece of text already in the drawing. The drawing has over 100 seperate pieces of text. I had tried using VB Sendkeys to send the commands to do a find and replace using the Autocad Find command, but Cad stops recieving the sent keys when the find dialogue comes up. Basically I am looking for the correct method of using VB to do a find and replace text within a Cad drawing. Any help greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Steven Bastiaanse Posted August 8, 2006 Share Posted August 8, 2006 Try this: Sub ChangeText() Dim Selection As AcadSelectionSet Dim Text As AcadText Dim SearchText As String Dim ReplaceText As String Dim FilterType(0) As Integer Dim FilterData(0) As Variant FilterType(0) = 0 FilterData(0) = "TEXT" On Error Resume Next Set Selection = ThisDrawing.SelectionSets.Item("ssText") If Err Then Set Selection = ThisDrawing.SelectionSets.Add("ssText") Err.Clear Else Selection.Clear End If 'Select the text. Selection.Select acSelectionSetAll, , , FilterType, FilterData SearchText = ThisDrawing.Utility.GetString(True, "Enter the search string: ") 'If text=Text. For Each Text In Selection If Text.TextString = SearchText Then ReplaceText = ThisDrawing.Utility.GetString(True, "Enter the replace string: ") Text.TextString = ReplaceText End If Next Text End Sub Greetings. Quote Link to comment Share on other sites More sharing options...
DDonnachie Posted August 8, 2006 Author Share Posted August 8, 2006 Thanks very much Steven, I will try this at work tomorrow. I should have posted this question last week instead of meesin about in the dark, lol Quote Link to comment Share on other sites More sharing options...
DDonnachie Posted August 9, 2006 Author Share Posted August 9, 2006 Ok I must be misundestanding something as my text isn't changing. Is the "ssText" a variable I need to change or is this something Autocad understands ? does this need something to apply the changes to the drawings ? Quote Link to comment Share on other sites More sharing options...
Steven Bastiaanse Posted August 9, 2006 Share Posted August 9, 2006 Not you don't need the change "ssText". I think I know what the problem is. The code above only changes Text, the text in you're drawing is probably MText. Try this one: Sub ChangeText() Dim Selection As AcadSelectionSet Dim MText As AcadMText Dim SearchText As String Dim ReplaceText As String Dim FilterType(0) As Integer Dim FilterData(0) As Variant FilterType(0) = 0 FilterData(0) = "MTEXT" On Error Resume Next Set Selection = ThisDrawing.SelectionSets.Item("ssText") If Err Then Set Selection = ThisDrawing.SelectionSets.Add("ssText") Err.Clear Else Selection.Clear End If 'Select the text. Selection.Select acSelectionSetAll, , , FilterType, FilterData SearchText = ThisDrawing.Utility.GetString(True, "Enter the search string: ") 'If text=Text. For Each MText In Selection If MText.TextString = SearchText Then ReplaceText = ThisDrawing.Utility.GetString(True, "Enter the replace string: ") MText.TextString = ReplaceText End If Next MText End Sub Quote Link to comment Share on other sites More sharing options...
DDonnachie Posted August 10, 2006 Author Share Posted August 10, 2006 Sorry I just tried both bits of code again, running slighlty modified both didn't work. So tried both again un modified this time pasted strainght into the drawing and the first one works, Sorry for messing you about Steven as your code works like its supposed to. Just going to re-check my changes, find out where I ballsed it up. So thanks for your help Quote Link to comment Share on other sites More sharing options...
DDonnachie Posted August 10, 2006 Author Share Posted August 10, 2006 Ok got it working now, not sure why but it didn't like me changing the Variable names of Selection to TextSelect or Text to TxtObj. Once I changed these back to yours it worked great. Thanks Steven, you've been a great help mate Quote Link to comment Share on other sites More sharing options...
Broman Posted November 21, 2006 Share Posted November 21, 2006 Are you firing these up in AutoCAD or in the Spreadsheet? I need a similar tool. We have around 600 part numbers that are going to change and these part numbers are peppered throughout several drawings. I'd like to be able to find all instances of Part No. A and replace with Part No. B and so on..... We have an Excel sheet with the old and new part numbers - and we planned on doing some sort of batch. Do you think this will help us? P.S. I am LISP and VBA illiterate - so be gentle. Thanks. Quote Link to comment Share on other sites More sharing options...
L11 Posted August 16, 2009 Share Posted August 16, 2009 This worked perfectly on my pc thanks! Not you don't need the change "ssText". I think I know what the problem is. The code above only changes Text, the text in you're drawing is probably MText. Try this one: Sub ChangeText() Dim Selection As AcadSelectionSet Dim MText As AcadMText Dim SearchText As String Dim ReplaceText As String Dim FilterType(0) As Integer Dim FilterData(0) As Variant FilterType(0) = 0 FilterData(0) = "MTEXT" On Error Resume Next Set Selection = ThisDrawing.SelectionSets.Item("ssText") If Err Then Set Selection = ThisDrawing.SelectionSets.Add("ssText") Err.Clear Else Selection.Clear End If 'Select the text. Selection.Select acSelectionSetAll, , , FilterType, FilterData SearchText = ThisDrawing.Utility.GetString(True, "Enter the search string: ") 'If text=Text. For Each MText In Selection If MText.TextString = SearchText Then ReplaceText = ThisDrawing.Utility.GetString(True, "Enter the replace string: ") MText.TextString = ReplaceText End If Next MText End Sub Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.