Dim iPt As Variant
Dim rh As Double
Dim cw() As Double
Sub test()
Dim xlApp As Object
Dim SSetObj As AcadSelectionSet
Dim fType(0 To 0) As Integer
Dim fData(0 To 0) As Variant
Dim EntObj As AcadEntity
Dim Pt As Variant
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.workbooks.Add
End If
Set SSetObj = ThisDrawing.SelectionSets("MXB")
If Err.Number <> 0 Then
Err.Clear
Set SSetObj = ThisDrawing.SelectionSets.Add("MXB")
End If
SSetObj.Clear
On Error GoTo ErrTrap
Dim xdType As Variant
Dim xdData As Variant
ThisDrawing.ModelSpace.GetXData "明细表表体", xdType, xdData
If IsEmpty(xdType) Then
Pt = ThisDrawing.Utility.GetPoint(, "指定明细表内容的左上角点: ")
ReDim xdType(0 To 1) As Integer
ReDim xdData(0 To 1) As Variant
xdType(0) = 1001
xdData(0) = "明细表表体"
xdType(1) = 1011
xdData(1) = Pt
ThisDrawing.ModelSpace.SetXData xdType, xdData
Else
Pt = xdData(1)
End If
iPt = Pt
SetRow
SetColumn
fType(0) = 0: fData(0) = "Text"
SSetObj.SelectOnScreen fType, fData
Dim r As Integer
Dim c As Integer
For Each EntObj In SSetObj
r = GetRow(EntObj)
c = GetColumn(EntObj)
If r > 0 And c > 0 Then xlApp.activesheet.cells(r, c) = EntObj.TextString
Next
Exit Sub
ErrTrap:
On Error GoTo 0
End Sub
Sub SetRow()
Dim xdType As Variant
Dim xdData As Variant
Dim Dist As Double
On Error GoTo ErrTrap
ThisDrawing.ModelSpace.GetXData "明细表行高", xdType, xdData
If IsEmpty(xdType) Then
Dist = ThisDrawing.Utility.GetDistance(iPt, "指定明细表的行高: ")
ReDim xdType(0 To 1) As Integer
ReDim xdData(0 To 1) As Variant
xdType(0) = 1001
xdData(0) = "明细表行高"
xdType(1) = 1040
xdData(1) = Dist
ThisDrawing.ModelSpace.SetXData xdType, xdData
Else
Dist = xdData(1)
End If
rh = Dist
Exit Sub
ErrTrap:
On Error GoTo 0
End Sub
Sub SetColumn()
Dim xdType As Variant
Dim xdData As Variant
Dim n As Integer
Dim Dist As Double
On Error GoTo ErrTrap
ThisDrawing.ModelSpace.GetXData "明细表列宽", xdType, xdData
If IsEmpty(xdType) Then
ReDim xdType(0 To 0) As Integer
ReDim xdData(0 To 0) As Variant
xdType(0) = 1001
xdData(0) = "明细表列宽"
Dist = -1
Do While Dist <> 0
Dist = ThisDrawing.Utility.GetDistance(iPt, "指定明细表的列宽: ")
If Dist <> 0 Then
n = n + 1
ReDim Preserve xdType(0 To n) As Integer
ReDim Preserve xdData(0 To n) As Variant
xdType(n) = 1040
xdData(n) = Dist
End If
Loop
ThisDrawing.ModelSpace.SetXData xdType, xdData
End If
ReDim cw(0 To UBound(xdType) - 1)
For n = 1 To UBound(xdType)
cw(n - 1) = xdData(n)
Next
Exit Sub
ErrTrap:
Dist = 0
Resume Next
On Error GoTo 0
End Sub
Function GetRow(ByVal EntObj As AcadText) As Integer
Dim tPt(0 To 2) As Double
Dim i As Integer
Dim n As Integer
On Error GoTo ErrTrap
tPt(0) = EntObj.InsertionPoint(0)
tPt(1) = EntObj.InsertionPoint(1) + 3
tPt(2) = 0
Do While iPt(1) + n * rh < tPt(1)
n = n + 1
Loop
GetRow = n
Exit Function
ErrTrap:
On Error GoTo 0
End Function
Function GetColumn(ByVal EntObj As AcadText) As Integer
Dim tPt(0 To 2) As Double
Dim i As Integer
Dim n As Integer
On Error GoTo ErrTrap
tPt(0) = EntObj.InsertionPoint(0) + 5
tPt(1) = EntObj.InsertionPoint(1)
tPt(2) = 0
Do While iPt(0) + cw(n) < tPt(0)
n = n + 1
Loop
GetColumn = n
Exit Function
ErrTrap:
On Error GoTo 0
End Function
其中,ipt保存了左下角点的坐标,rh保存了明细表的行高,cw数组保存了明细表的列宽(是指这一列与前面所有列的总宽度)。使用了扩展数据保存数据,因而上述的值只在第一次运行时会提示输入。
GetRow和GetColumn用于判断一个文字所在的行和列。
注意输出到Excel里的行顺序是倒着。