I am new to Acad VBA.
I would like to find a text object containing any string and replace all the text in the object.
for example...
1) Searching for "abc" found objects containing "123 abc 456"
2) Select all the text inside the "123 abc 456" object.
3) Replace "123 abc 456" with "789 abc 999"
How should I handle this situation?
Solved! Go to Solution.
Solved by ed57gmc. Go to Solution.
Have you tried the FIND command? It does exactly that.
I tried running the FIND command, but for '123 abc 456' it only replaces 'abc' and not '123' and '456'.
Execution result
1) Search with "*abc*"
2) Found "123 abc 456" ("123" and "456" are not fixed numbers.)
3) After doing the replacement, I get "123789 abc 999456".
Am I using the FIND command wrong?
What are you using for the replace string? It seems that "*abc*" found the text ok.
sorry
Search string: "*abc*"
String after replacement: "789 abc 999"
Search results for: "123 abc 456"
Substitution result: "123 789 abc 999 456"
With the current method, only "abc" can be replaced, but I want to replace "123 abc 456" entirely.
That seems fair. I don't know how new you are to VBA. There are lots of code on this forum. If you search for 'GetSS*' or "AddSelectionSet", I've posted several functions that filter for different entity types. Then its just a matter of iterating the selectionset with a for..each statement, editing the TextString property. If that's not enough, I can come up with something.
Here's my solution. For the find to work, you have to use the wild cards like "*abc*". As is, it searches the whole drawing. If you wanted to select the text to search, substitute the GetSS_TextFilter function with the GetSS_TextFilter_SOS function.
Public Sub ReplaceText()
Dim ss As AcadSelectionSet
Set ss = GetSS_TextFilter()
Dim sFind As String
Dim sReplace As String
sFind = ThisDrawing.Utility.GetString(1, "Enter the string to search for:> ")
sReplace = ThisDrawing.Utility.GetString(1, "Enter the replacement string:> ")
Dim oEnt As AcadEntity
Dim oTxt As AcadText
Dim oMtxt As AcadMText
For Each oEnt In ss
Select Case oEnt.ObjectName
Case Is = "AcDbText"
Set oTxt = oEnt
If oTxt.TextString Like sFind Then oTxt.TextString = sReplace
Case Is = "AcDbMText"
Set oMtxt = oEnt
If oMtxt.TextString Like sFind Then oMtxt.TextString = sReplace
End Select
Next oEnt
End Sub
Public Function GetSS_TextFilter() As AcadSelectionSet
'creates an ss of Text only
Dim s1 As AcadSelectionSet 'for filtered ss
'filter is needed if there's no pfss
Dim intFtyp(0) As Integer ' setup for the filter
Dim varFval(0) As Variant
Dim varFilter1, varFilter2 As Variant
intFtyp(0) = 0: varFval(0) = "TEXT,MTEXT" ' get only text and mtext
varFilter1 = intFtyp: varFilter2 = varFval
Set s1 = AddSelectionSet("ssTextFilter") ' create or get the set
s1.Clear ' clear the set
s1.Select acSelectionSetAll, , , varFilter1, varFilter2 ' do it
Set GetSS_TextFilter = s1
End Function
Public Function AddSelectionSet(SetName As String) As AcadSelectionSet
' This routine does the error trapping neccessary for when you want to create a
' selectin set. It takes the proposed name and either adds it to the selectionsets
' collection or sets it.
On Error Resume Next
Set AddSelectionSet = ThisDrawing.SelectionSets.Add(SetName)
If Err.Number <> 0 Then
Set AddSelectionSet = ThisDrawing.SelectionSets.Item(SetName)
AddSelectionSet.Clear
End If
End Function
Public Function GetSS_TextFilter_SOS() As AcadSelectionSet
'creates an ss of Text only using SelectOnScreen
Dim s1 As AcadSelectionSet 'for filtered ss
'filter is needed if there's no pfss
Dim intFtyp(0) As Integer ' setup for the filter
Dim varFval(0) As Variant
Dim varFilter1, varFilter2 As Variant
intFtyp(0) = 0: varFval(0) = "TEXT,MTEXT" ' get only text and mtext
varFilter1 = intFtyp: varFilter2 = varFval
Set s1 = AddSelectionSet("ssTextFilter") ' create or get the set
s1.Clear ' clear the set
s1.SelectOnScreen varFilter1, varFilter2 ' do it
Set GetSS_TextFilter_SOS = s1
End Function
thank you very much!
I got better results than I expected.
I used Excel VBA for many years, so I knew the settings, but I didn't know what to look for and what to get.
I will do my best to study AcadVBA based on the source I received!
Can't find what you're looking for? Ask the community or share your knowledge.