[分享][讨论]VBA与电子表格
本帖最后由 作者 于 2003-10-11 10:00:07 编辑大家经常讨论VBA与数据库,却有很少人讨论VBA与EXCEL,我在学这方面的内容,感觉势单力薄啊,特发表下面一段范例程序,望引起各位朋友的兴趣一起探讨之。
本程序是查找块参考的属性,并将取得的属性值转到EXCEL中。
Sub vbaexcelsj()
'定义excel.application类型的对象变量
Dim excelobj As Excel.Application
Dim excelsheet As Object
Dim excelworkbook As Object
Dim rownumber As Integer
Dim headertf As Boolean
Dim elemobj As AcadEntity
Dim arraydata As Variant
'启动excelobj
Set excelobj = New Excel.Application
'创建工作簿和寻找作用中的表
Set excelworkbook = excelobj.workbooks.Add
Set excelsheet = excelobj.activesheet
excelworkbook.SaveAs "attribute.xls"
rownumber = 1
hesdertf = False
'遍历寻找所有图块
For Each elemobj In ThisDrawing.ModelSpace
With elemobj
' 发现块参考时,检查其属性
If StrComp(.EntityName, "acdbblockreference", 1) = 0 Then
If .HasAttributes Then
'取得属性值
arraydata = .GetAttributes
'复制属性的标签字符串到excel
For appcount = LBound(arraydata) To UBound(arraydata)
If headertf = False Then
If StrComp(arraydata(appcount).EntityName, "acdbattribute", 1) = 0 Then
excelsheet.cells(rownumber, appcount + 1).Value = arraydata(appcount).TagString
End If
End If
Next appcount
rownumber = rownumber + 1
For appcount = LBound(arraydata) To UBound(arraydata)
excelsheet.cells(rownumber, appcount + 1).Value = arraydata(appcount).TextString
Next appcount
headertf = True
End If
End If
End With
Next elemobj
excelobj.Application.Quit
End Sub
在本版块成立之际,现将源程序也传上来
VBA开发指南 不错! 请问,该程序的编译是在cad环境,还是在excel的vba环境中执行?? 在AUTOCAD的VBA环境下,不过你不要忘记了引用MICROSOFT EXCEL哦,生成的EXCEL在我的文档目录下面。 好东东~~~~~~ OK!谢谢! excelsheet.cells(rownumber, appcount + 1).value = arraydata(appcount).TextString
excellsheet 指电子表格的sheets中的哪一个? excelapp.activesheet 我好象在哪本书中看到过?不记得了,好象AUTOCAD VBA开发中的CAD与EXCEL通信 是一个范例,李凤华编的。
好象是开发指南吧。清华大学出的。
页:
[1]
2