VBA string replace mtext

VBA string replace mtext

jorgen.fidjeland
Explorer Explorer
4,205 Views
5 Replies
Message 1 of 6

VBA string replace mtext

jorgen.fidjeland
Explorer
Explorer

Hi,

 

So I have a autocad 2d drawing with a lot of mtext. I have to change all instances of ":" to "-". Doing this manually would take a long time, so I thought of doing it with VBA. 


I'm not asking for a full code, but some input on wether this is possible or not? All I have found about this on the internet is about making a new mtext, not editing existing mtexts.

 

So, my pseudo-code I imagine is something like this

 

Loop all objects

if(type=mtext)

       mtext.text = str_replace(":","-",mtext.text)

end if



This is first time vba in acad. Have done some before in excel and access..

Thanks in advance


 

0 Likes
4,206 Views
5 Replies
Replies (5)
Message 2 of 6

Ed__Jobe
Mentor
Mentor

Sure, its possible, but have you tried the FIND command?

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 3 of 6

naresh_kalyan
Advocate
Advocate

Hi all,

I have tried to get the MText string of a Mtext of AutoCAD 2016 using VBA. Here the case is, I could able to get "Text" but not MText. Kindly anyone help me. I'm bit new to AutoCAD customization.

 

Set oSset = .Add("$MTexts$").

 

 

Dim mode As Integer
Dim corner1(0 To 2) As Double
Dim corner2(0 To 2) As Double

mode = acSelectionSetCrossing
corner1(0) = 3.1325: corner1(1) = 2.1875: corner1(2) = 0 ''2 3/16
corner2(0) = 3.6315: corner2(1) = 3.5625: corner2(2) = 0
oSset.Select mode, corner1, corner2

 

please help me...

 

Regards

Naresh Kalyan

0 Likes
Message 4 of 6

norman.yuan
Mentor
Mentor

Well, your code is not relevant to why you was able to get Text, but not MText. It just selects anything inside/crossed by a window. Are you sure there are MText, or any entity for that matter, being selected in the SelectionSet? Since you did not set selecting filter, it is not guaranteed that only MText entities are selected. So, you need to find out if an entity in the SelectionSet is the type of entity you target:

 

...

'' Assume the selection does select some entities

'' which may or may not be all MText entities

OSset.Select mode, corner1, corner2

Dim ent As AcadEntity

Dim mtxt As AcadMText

Dim count As Integer

For Each ent In oSset

    If TypeOf ent Is AcadMText Then

        Set mtxt=ent

        HandleMText mtxt

        count = count +1

    End If

Next

MsgBox count & "MText entities have been handled."

....

 

Private Sub HandleMText(mtext As AcadMText)

    '' Do whatever with the MText

End Sub

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 6

naresh_kalyan
Advocate
Advocate

Thank you for the help with in a short span... it works perfectly.. 

Thank you Norman...

 

Regards

Naresh Kalyan

 

0 Likes
Message 6 of 6

Anonymous
Not applicable

Hi Naresh,

 

This Mahamuni from Dombivali Mumbai sudhanvammahamuni@gmail.com. Please go through following link, hope this will be helpful for you......

https://myengineeringworld.net/2014/03/add-text-in-autocad-using-excel-vba.html

This program is developed by Mr. Christos from Greece. He is very helpful and if he know the solution he will defiantly help you,  you will find his contact details on his website. 

 

Mahamuni


@jorgen.fidjeland wrote:

Hi,

 

So I have a autocad 2d drawing with a lot of mtext. I have to change all instances of ":" to "-". Doing this manually would take a long time, so I thought of doing it with VBA. 


I'm not asking for a full code, but some input on wether this is possible or not? All I have found about this on the internet is about making a new mtext, not editing existing mtexts.

 

So, my pseudo-code I imagine is something like this

 

Loop all objects

if(type=mtext)

       mtext.text = str_replace(":","-",mtext.text)

end if



This is first time vba in acad. Have done some before in excel and access..

Thanks in advance


@jorgen.fidjeland wrote:

Hi,

 

So I have a autocad 2d drawing with a lot of mtext. I have to change all instances of ":" to "-". Doing this manually would take a long time, so I thought of doing it with VBA. 


I'm not asking for a full code, but some input on wether this is possible or not? All I have found about this on the internet is about making a new mtext, not editing existing mtexts.

 

So, my pseudo-code I imagine is something like this

 

Loop all objects

if(type=mtext)

       mtext.text = str_replace(":","-",mtext.text)

end if



This is first time vba in acad. Have done some before in excel and access..

Thanks in advance


 





 

 

0 Likes