Jump to content

Change text contents using VB


DDonnachie

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

  • 3 months later...

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.

Link to comment
Share on other sites

  • 2 years later...

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

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...