这个我写过,和楼主所说的功能一模一样。对于属性文字也可以(不过属性中只提取第一个文字),不过对于Mtext没有试过,不知道好不好用。代码如下:
'主程序如下
Sub SameText() ThisDrawing.Utility.Prompt "欢迎使用《文字变相同》"
Dim getobj1 As Object
Dim getObj2 As Object
Dim basePnt As Variant
Dim getaReal As Variant
Dim ssetobj As AcadSelectionSet '声明一个集合
Dim Att1 As Variant '声明一个属性变量
Dim Att2 As Variant
On Error Resume Next
ThisDrawing.SelectionSets("被改变文字").Delete
Set ssetobj = ThisDrawing.SelectionSets.Add("被改变文字")
On Error GoTo Finish
gwGetEntity getobj1, basePnt, "选择被复制文字或属性:", "AcDbBlockReference", "AcDb*text"
If getobj1 Is Nothing Then GoTo Finish
Dim FType, FData
BuildFilter FType, FData, -4, ""
ssetobj.SelectOnScreen FType, FData
If ssetobj.Count = 0 Then GoTo Finish '如果没有选择物体,结束程序
Dim textStr As String
If getobj1.ObjectName = "AcDbBlockReference" Then
Att1 = getobj1.GetAttributes()
textStr1 = Att1(0).TextString
ElseIf getobj1.ObjectName = "AcDbText" Then
textStr1 = getobj1.TextString
End If
For Each pickedObjs In ssetobj
If pickedObjs.ObjectName = "AcDbBlockReference" Then
Att2 = pickedObjs.GetAttributes()
Att2(0).TextString = textStr1
Else
pickedObjs.TextString = textStr1
End If
NextFinish:
ssetobj.Delete
End Sub'函数如下Public Sub gwGetEntity(ent As Object, pickedPoint, Prompt As String, ParamArray gType())
'选择某一类型的实体,如果选择错误则继续,按ESC退出
'gtype是实体名称,不区分大小写,可以用通配符号,如"AcDbBlockReference","acdb*text"等
Dim i As Integer
Dim pd As Boolean
pd = False
Do
GetEntityEx ent, pickedPoint, Prompt
If ent Is Nothing Then
Exit Do
ElseIf UBound(gType) - LBound(gType) + 1 = 0 Then
Exit Do
Else
For i = LBound(gType) To UBound(gType)
If UCase(ent.ObjectName) Like UCase(gType(i)) Then
Exit Do
Else
pd = True
End If
Next i
If pd Then ThisDrawing.Utility.Prompt "选择的实体不符合要求."
End If
Loop
End Sub
Public Sub GetEntityEx(ent As Object, pickedPoint, Optional Prompt)
'选择实体,直到用户取消操作
On Error Resume Next
StartLoop:
ThisDrawing.Utility.GetEntity ent, pickedPoint, Prompt
If Err Then
If ThisDrawing.GetVariable("errno") = 7 Then
Err.Clear
GoTo StartLoop
Else Err.Raise vbObjectError + 5, , "用户取消操作"
End If
End IfEnd Sub
Public Sub BuildFilter(typeArray, dataArray, ParamArray gCodes())
'用数组方式填充一对变量以用作为选择集过滤器使用
Dim FType() As Integer, FData()
Dim index As Long, i As Long
index = LBound(gCodes) - 1
For i = LBound(gCodes) To UBound(gCodes) Step 2
index = index + 1
ReDim Preserve FType(0 To index)
ReDim Preserve FData(0 To index)
FType(index) = CInt(gCodes(i))
FData(index) = gCodes(i + 1)
Next
typeArray = FType: dataArray = FData
End Sub