How to find and replace a text using VBA?

How to find and replace a text using VBA?

shuaib_cad
Advocate Advocate
6,363 Views
23 Replies
Message 1 of 24

How to find and replace a text using VBA?

shuaib_cad
Advocate
Advocate

I want to use the find and replace option to replace a Mtext. The details are as follows:

Existing Mtext: AAAAA

New Mtext: 12345

 

 

Regards,

Mohammed Shuaib

0 Likes
6,364 Views
23 Replies
Replies (23)
Message 2 of 24

grobnik
Collaborator
Collaborator

@shuaib_cad there are a lot of way to search and replpace text, here a simple code:

Sub MtextChange()
For Each ENTITY In ThisDrawing.ModelSpace  'For each entity inside modele space                                      
    If ENTITY.ObjectName = "AcadMtext" Then                                        
        If ENTITY.textstring= "XXXX" Then  ' check text inside the dwg
           ENTITY.textstring="12345"
        End If
    End If
Next
End Sub

Try and let us know

Message 3 of 24

chris.slattery
Contributor
Contributor

I know thread is from a year ago. I tried this code and it does not work when I run it. I am looking for something like this too. I am trying to change a piece of text that is in both single line text and mtext.

0 Likes
Message 4 of 24

grobnik
Collaborator
Collaborator

@chris.slattery 

try in this way

Sub MtextChange()
For Each ENTITY In ThisDrawing.ModelSpace  'For each entity inside modele space
    If TypeOf ENTITY Is AcadMText Then
        If ENTITY.TextString = "XXXX" Then ' check text inside the dwg
           ENTITY.TextString = "12345"
        End If
    End If
Next
ThisDrawing.Application.Update
End Sub
0 Likes
Message 5 of 24

chris.slattery
Contributor
Contributor

ok. I got this to work but it only works if the XXXX is the only text in an mtext. It does not work on single line text or if the XXXX is in a sentence.

0 Likes
Message 6 of 24

Ed__Jobe
Mentor
Mentor

The code he gave you is just a sample. You should be able to construct a test for AcadText like he did for AcadMText.

 

For the search, don't use the = operator, you get what you ask for. Use the LIKE operator.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 7 of 24

grobnik
Collaborator
Collaborator

@chris.slattery Ok, sorry I didn't read the entire message,

you can try with 

If typeOf ENTITY Is AcadMText or typeOf ENTITY Is AcadText then
----

In addition for replacing the partial part of string (ENTITY.textstring) you can use a lot of string manipulation function (left, right, mid, instr etc), and rewrite the ENTITY.textstring

I don't know in deep your requirements, so please be flexible.

0 Likes
Message 8 of 24

maratovich
Advisor
Advisor

If you can't code, then maybe use a ready-made solution?
Write about it.

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 9 of 24

chris.slattery
Contributor
Contributor

My coding skill for AutoCad LSP and VBA are minimal. I can do something simple. But anything else I need to search for something I can use. But I am trying to learn more. I just do not have to automate any tasks of the time, so code writing is sporadic. I have more experience with VBA for Microstation. I have written one like this for Microstation, the code is very similar, but text is different. AC has both single line and multiline, Microstation does not have that. The reason I am not using the find and replace text is I am going to need to run this in batch. We have utility that will apply this to many files.

 

 

Message 10 of 24

Ed__Jobe
Mentor
Mentor

You just have to figure out the logic to identify two conditions rather than the one condition that @grobnik showed you. Do you know how the OR operator works? Look in the VBA Help index under Operators.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 11 of 24

chris.slattery
Contributor
Contributor

I added this and added the rewrite. Now is will change the text in both single line and mtext. But only if the XXXX is the only thing in the string. If there is other text it did not work.

 

Sub MtextChange()
For Each ENTITY In ThisDrawing.ModelSpace 'For each entity inside modele space
If TypeOf ENTITY Is AcadMText Or TypeOf ENTITY Is AcadText Then
If ENTITY.TextString = "XXXX" Then ' check text inside the dwg
ENTITY.TextString = "12345"
rewrite.ENTITY.TextString
End If
End If
Next
ThisDrawing.Application.Update
End Sub

0 Likes
Message 12 of 24

Ed__Jobe
Mentor
Mentor

See my reply in post 6.

"For the search, don't use the = operator, you get what you ask for. Use the LIKE operator."

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 13 of 24

grobnik
Collaborator
Collaborator

Hi @chris.slattery , as I already wrote in my message and Ed wrote in own message:
In addition for replacing the partial part of string (ENTITY.textstring) you can use a lot of string manipulation function (left, right, mid, instr etc), and rewrite the ENTITY.textstring. I guess string manipulation function are standard VBA code, not dedicated to Autodesk, so I guess MICROSTATION VBA should be very similar.
So you got the way to search a string with Like or exact TEXT OR MTEXT to search, or you can use the string manipulation for searching text too.... for example ..... if LEFT$(ENTITY.textstring, NUMBER OF CHAR) = "XXX" or LIKE FUNCTION THEN ..... DO SOMETHING.

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/like-operator
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/left-function

As advanced option you can use the user form for text search and replace by VBA (more complex as code).
You have to test, investigate on line help for VBA or free VBA training on line, forum help... we can help you but again we don't know exactly your requirements.
Bye

0 Likes
Message 14 of 24

Ed__Jobe
Mentor
Mentor

@grobnik wrote:
Hi Chris, as I already wrote in my message and Ed wrote in own message:
In addition for replacing the partial part of string (ENTITY.textstring) you can use a lot of string manipulation function (left, right, mid, instr etc), and rewrite the ENTITY.textstring. I guess string manipulation function are standard VBA code, not dedicated to Autodesk, so I guess MICROSTATION VBA should be very similar.
So you got the way to search a string with Like or exact TEXT OR MTEXT to search, or you can use the string manipulation for searching text too.... for example ..... if LEFT$(ENTITY.textstring, NUMBER OF CHAR) = "XXX" or LIKE FUNCTION THEN ..... DO SOMETHING.

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/like-operator
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/left-function

As advanced option you can use the user form for text search and replace by VBA (more complex as code).
You have to test, investigate on line help for VBA or free VBA training on line, forum help... we can help you but again we don't know exactly your requirements.
Bye

You're still using =.

If entity.TextString LIKE "XXXX"

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 15 of 24

maratovich
Advisor
Advisor

@chris.slattery  написал (-а):

 The reason I am not using the find and replace text is I am going to need to run this in batch. We have utility that will apply this to many files.


Then maybe this will help you - AutoReplaceCAD 

 

 

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
0 Likes
Message 16 of 24

chris.slattery
Contributor
Contributor

I added the LIKE with the quotes around what it is looking for and now it changes both single and multiline as long as the XXXX is all that is in the string. I tried the other operators to see what they do and the only one that almost worked was the wildcard *. That replaced an entire sentence that has the XXXX in it with the 12345.

0 Likes
Message 17 of 24

grobnik
Collaborator
Collaborator
So it's enough for you? Try mid$(Entitiy.textstring, start point, n of chars) or left or right or instr a lot of string manipulation functions.
0 Likes
Message 18 of 24

chris.slattery
Contributor
Contributor

Mid still replaces the entire string of text. It seems there is no way to get it to work the same way as Find and Replace Text.

0 Likes
Message 19 of 24

grobnik
Collaborator
Collaborator
Not exactly, with mid you can search from a single char to entire lenght of string.
The same on replace if you want to replace only a part of your string store the found entire text in a variable, and replace only a part with mid function.
0 Likes
Message 20 of 24

chris.slattery
Contributor
Contributor

Ok. That answers a lot.

I have to replace a character that is in a few different text strings with a different character. None of the text strings are the same but this character is and it needs to be done on about a hundred files.

 

Is there a way to do this with lsp so it works like the find and replace tool? We use Hurricane, so I want to apply it to all of these files at one time.

0 Likes