VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can I change the textstring of an expected AcadText by vba?

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
3083 Views, 5 Replies

How can I change the textstring of an expected AcadText by vba?

Hello , I want to change the textstring parameter of some AcadText object by vba.
1. how to select the expected object ? All objects in drawings have a name?
2. how to change the textstring parameter?
thanks...

5 REPLIES 5
Message 2 of 6
RICVBA
in reply to: Anonymous

you could select a text object via on of selectionset methods, by specifying both "TEXT" as entity type (group code "0") and the text you're interested in (group code 1)

 

once you grab a Text object, you can modify its textstring via the "TextString" property

 

for instance

Option Explicit

Sub SelText()

Dim entObj As AcadEntity
Dim ssetObj As AcadSelectionSet
Dim grpCode(1) As Integer
Dim dataVal(1) As Variant
Dim AcdText As AcadText

'define selectionset criteria
grpCode(0) = 0: dataVal(0) = "TEXT" ' only text objects
grpCode(1) = 1: dataVal(1) = "a*" ' only those objects with values (in this case "textstring") beginning with "a"

' create the selectionset
On Error Resume Next
Set ssetObj = ThisDrawing.SelectionSets.Item("MYTEXTS")
If Err Then Set ssetObj = ThisDrawing.SelectionSets.Add("MYTEXTS")
On Error GoTo 0

'fill the selectionset
ssetObj.Select acSelectionSetAll, , , grpCode, dataVal


' handle text objects you collected
For Each AcdText In ssetObj
    
    AcdText.TextString = AcdText.TextString & "-1" ' just for example: modify its textstring by concatenating "-1" at its end

Next AcdText

ssetObj.Delete

End Sub

 

Message 3 of 6
Anonymous
in reply to: RICVBA

thanks RICVBA .

I'v read you program,but i'v not test it .

it can findout the text object that with some special characteristic , as textstring start with "a", it seems like "replace" function in word/excel.

but how to findout any text object and modify it ,which i want to ,

just like i modify a cell in excel,or modify the caption of a lable  via script in vb frame.

thank you again...

Message 4 of 6
RICVBA
in reply to: Anonymous

I'm afraid I'm not getting your poiint

but I can add what follows to what already shown you: just have remove the 2nd selection criterion to have all text objects of your drawing collected in the selectionset.

i.e. just change the following code snippets as shown

Dim grpCode(0) As Integer
Dim dataVal(0) As Variant

 

grpCode(0) = 0: dataVal(0) = "TEXT" ' only text objects
'grpCode(1) = 1: dataVal(1) = "a*" ' only those objects with values (in this case "textstring") beginning with "a"

and from then on, just enter the "for each-next" loop to manage every single text object as you like

 

if it doesn't help, you must be more specific about your goal, possibly attacching the dwg file (autocad 2010 or older) you want to process and your text modification criteria

Message 5 of 6
Anonymous
in reply to: RICVBA

Thanks.
1. my requirement: can modify any text's textstring via vba script , autocad 2013.
2. the text don't have any clear characteristic, they are used to show some information,like a valve's name, a instrument's value ( temp or flowrate)
3.dose the text object have a name in dwgfile,so i can Reference it.
i.e. scrpt : thisdwgfile.mytext.textstring = "newstring"
can autocad do this?
4. in you description, how to select the object in the "for each next"loop?
Message 6 of 6
RICVBA
in reply to: Anonymous

1. yes. use"Textstring" property of AcadText object

 

2 if there's no clear characteristic useful to identify a text, I guess you can only select it:

- on the screen via some selection method like "SelectOnScreen" or the likes. here's its example in the AutoCAD ActiveX and VBA Reference

Sub Example_SelectOnScreen()
    ' This example adds objects to a selection set by prompting the user
    ' to select ones to add.
    
    ' Create the selection set
    Dim ssetObj As AcadSelectionSet
    Set ssetObj = ThisDrawing.SelectionSets.Add("TEST_SSET")
    
    ' Add objects to a selection set by prompting user to select on the screen
    ssetObj.SelectOnScreen
    
End Sub

 

 

- via "GetString" method, by which you would get a string text from the user. here's its example in the AutoCAD ActiveX and VBA Reference

Sub Example_GetString()
    ' This example demonstrates different ways of returning a string
    ' entered by a user.
    
    Dim returnString As String
    
    ' Prompt & Input cannot contain blanks
    returnString = ThisDrawing.Utility.GetString(False, "Enter text (a space or <enter> terminates input): ")
    MsgBox "The string entered was '" & returnString & "'", , "GetString Example"
    
    ' Prompt & Input can contain blanks
    returnString = ThisDrawing.Utility.GetString(True, "Enter text (<enter> terminates input):")
    MsgBox "The string entered was '" & returnString & "'", , "GetString Example"
    
    ' Prompt & Input can contain blanks, but not an empty string
    Dim NoNull As Integer
    NoNull = 1    ' Disallow null
    ThisDrawing.Utility.InitializeUserInput NoNull
    returnString = ThisDrawing.Utility.GetString(True, "Enter text (<enter> terminates input): ")
    MsgBox "The string entered was '" & returnString & "'", , "GetString Example"

End Sub

 and then use it to search the drawing for text object matching it

 

3 any textobject (like any object) has both a "ObjectID" and a "Handle" property, which can serve as a unique reference in the drawing. I'd stick to the latter since "it stays the same for the lifetime of the object" as says AutoCAD ACtiveX and VBA Reference. while the "ObjectID" property is recalculated everytime you open the drawing.

 

4. within the "for each - next" loop, the variable "AcdText" is set as the current text object under processing

so for instance you can code "myHandle=AcdText.Handle" to retrieve its Handle property and store it in "myHandle" string variable for your subsequent purposes

 

hope what above can set you on the launching pad!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost