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...
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
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...
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
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!