How to find and replace a text using VBA?

How to find and replace a text using VBA?

shuaib_cad
Advocate Advocate
6,496 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,497 Views
23 Replies
Replies (23)
Message 21 of 24

grobnik
Collaborator
Collaborator

@chris.slattery 

The below code search for a string with the third char containing "3", and replace the third char with "-ZZ" leaving the first 2 chars of found string and from the third to total length of string as previously.

When I'm saying "rewrite" I didn't mean the command rewrite (I guess it's not existing....) but I meant assign to object found a different value. Again this is only an example, if your string to search it's not a constant you can ask on the command bar to insert the string to search or partial string to search

' Prompt & Input can contain blanks
	returnString = ThisDrawing.Utility.GetString(True, "Enter text (<enter> terminates input):")
	MsgBox "The string entered was '" & returnString & "'", , "GetString Example"

If ENTITY.TEXTSTRING=returnString then........

if MID$(ENTITY.TEXTSTRING, Starting point in the string, N of char) = returnString then ....

or using like

MyCheck = "aBBBa" Like "a*a"    ' Returns True.
MyCheck = "F" Like "[A-Z]"    ' Returns True.
MyCheck = "F" Like "[!A-Z]"    ' Returns False.
MyCheck = "a2a" Like "a#a"    ' Returns True.
MyCheck = "aM5b" Like "a[L-P]#[!c-e]"    ' Returns True.
MyCheck = "BAT123khg" Like "B?T*"    ' Returns True.
MyCheck = "CAT123khg" Like "B?T*"    ' Returns False.

 

 

 

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 Mid$(ENTITY.TextString, 3, 1) = "3" Then ' check text inside the dwg
        A = Mid$(ENTITY.TextString, 1, 2)
        B = Mid$(ENTITY.TextString, 4, Len(ENTITY.TextString))
        
        ENTITY.TextString = A & "-ZZ-" & B
    
    End If
End If
Next
ThisDrawing.Application.Update
End Sub

 

 

 

 

0 Likes
Message 22 of 24

chris.slattery
Contributor
Contributor

do you if there is a way to do this with lsp or will it be the same.

0 Likes
Message 23 of 24

grobnik
Collaborator
Collaborator

No I'm sorry I don't use lsp, there is a site with a lot of lsp function http://www.lee-mac.com/

0 Likes
Message 24 of 24

chris.slattery
Contributor
Contributor

I downloaded the Lee Mac tool. This will do what I need. I can select a folder full of files. I would like to thank everyone for helping.

0 Likes