本帖最后由 作者 于 2007-12-9 21:32:46 编辑
AutoCAD2006以上版本使用属性块的方法如下 ' 导出到Word中 Public Sub OutputToWord(ByVal SSetObj As AcadSelectionSet, ByVal LBObj As ListBox) Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim wdTable As Word.Table Dim EntObj As AcadEntity Dim AttRefObjs As Variant Dim n As Integer Dim i As Integer Dim j As Integer On Error Resume Next ' 连接Word Set wdApp = GetObject(, "Word.Application") If Err Then Err.Clear Set wdApp = CreateObject("Word.Application") If Err Then Err.Clear MsgBox "无法启动Word,请检查是否正确安装!" Exit Sub End If End If wdApp.Visible = True On Error GoTo ErrTrap ' 返回新创建的文档 Set wdDoc = wdApp.Documents.Add ' 返回在段落一之后新创建的表格 Set wdTable = wdDoc.Tables.Add(wdDoc.Paragraphs(1).Range, 1, LBObj.ListCount) n = 0 ' 遍历选择集 For Each EntObj In SSetObj ' 增加行 wdTable.Rows.Add ' 返回属性数据 AttRefObjs = EntObj.GetAttributes n = n + 1 For i = 0 To UBound(AttRefObjs) For j = 0 To LBObj.ListCount - 1 If AttRefObjs(i).TagString = LBObj.List(j) And LBObj.Selected(j) = True Then If n = 1 Then ' 首行,标签做为表格的列标题 wdTable.Cell(n, j + 1).Range.Text = AttRefObjs(i).TagString wdTable.Cell(n + 1, j + 1).Range.Text = AttRefObjs(i).TextString Else wdTable.Cell(n + 1, j + 1).Range.Text = AttRefObjs(i).TextString End If End If Next Next Next ' 删除表格中的空列 For i = LBObj.ListCount - 1 To 0 Step -1 If wdTable.Cell(1, i + 1).Range.Text = vbCr + Chr(7) Then wdTable.Columns(i + 1).Delete End If Next ' 按序号排序 wdTable.Sort True, "列 1" ' 自动调整列宽 wdTable.AutoFitBehavior 1 ' 释放Word对象 Set wdTable = Nothing Set wdDoc = Nothing Set wdApp = Nothing Exit Sub ErrTrap: On Error GoTo 0 End Sub ' 导出到Excel中 Public Sub OutputToExcel(ByVal SSetObj As AcadSelectionSet, ByVal LBObj As ListBox) Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim EntObj As AcadEntity Dim AttRefObjs As Variant Dim n As Integer Dim i As Integer Dim j As Integer On Error Resume Next ' 连接Excel Set xlApp = GetObject(, "Excel.Application") If Err Then Err.Clear Set xlApp = CreateObject("Excel.Application") If Err Then Err.Clear MsgBox "无法启动Word,请检查是否正确安装!" Exit Sub End If End If xlApp.Visible = True On Error GoTo ErrTrap ' 返回新创建的工作簿 Set xlBook = xlApp.Workbooks.Add ' 返回新增加的工作表,并移动到最后一个 Set xlSheet = xlBook.Worksheets.Add xlSheet.Move , xlBook.Worksheets(xlBook.Worksheets.Count) n = 0 ' 遍历选择集 For Each EntObj In SSetObj ' 返回属性数据 AttRefObjs = EntObj.GetAttributes n = n + 1 For i = 0 To UBound(AttRefObjs) For j = 0 To LBObj.ListCount - 1 If AttRefObjs(i).TagString = LBObj.List(j) And LBObj.Selected(j) = True Then If n = 1 Then ' 首行,标签做为表格的列标题 xlSheet.Cells(n, j + 1).Value = AttRefObjs(i).TagString xlSheet.Cells(n + 1, j + 1).Value = AttRefObjs(i).TextString Else xlSheet.Cells(n + 1, j + 1).Value = AttRefObjs(i).TextString End If End If Next Next Next ' 删除表格中的空列 For i = LBObj.ListCount - 1 To 0 Step -1 If xlSheet.Cells(1, i + 1).Value = "" Then xlSheet.Columns(i + 1).Delete End If Next ' 按序号排序 xlSheet.UsedRange.Sort Key1:=Range("A2"), Header:=xlYes ' 自动调整列宽 xlSheet.Columns.AutoFit ' 释放Exccel对象 Set xlSheet = Nothing Set xlBook = Nothing Set xlApp = Nothing Exit Sub ErrTrap: On Error GoTo 0 End Sub Excel VBA 范例文件代码 |