|
|
Sub list() , m. G0 e( ?6 c: W
Dim work As Workspace
T1 W3 \. o/ k; `' `: LDim new As Database
1 w$ y s2 _: q6 z- H! DDim elem As Object
0 A4 _% Q8 b* I6 S/ }Dim rs As Recordset
8 Y, C5 V$ D- H0 s0 qDim RowNum As Integer 3 ^$ F0 N* ?7 x0 t/ E6 m
Set work = DBEngine.Workspaces(0) 3 z# t5 d9 K- D3 {
Dim dbs As Database
) ?3 K# R& ]' G% SDim tdfNew As TableDef
& Q3 I- f+ ]$ T, QDim tdf As TableDef
/ X1 H. Q2 \: X! K8 ?9 x6 jDim dbsname As String ; s% X7 l8 Y$ F" I, o$ A" I
Dim array1 As Variant / q& ^+ `/ q4 Z* s& a
Dim array2 As Variant ‘声明所需的变量及类型 7 I& }4 j3 X8 {2 V
dbsname = “D:\材料表.mdb”
6 j3 c/ e* l0 ]9 _1 O. |‘声明Access数据库写到哪一个文件
! g8 S1 [: K8 S$ ^$ z' L+ GOn Error Resume Next
- `" C8 \. Q/ Z* {/ A( G3 T2 R% eSet dbs = work.CreateDatabase(dbsname, _ + e2 R, o# \) E+ N' z
dbLangGeneral) 2 z2 N# B; o; ^3 q0 G
If Err Then 9 P* O* e" k+ }. n0 d
Kill (dbsname)
# I* z' c! ^: g/ \. n‘发现要写入的Access数据库文件已存在就将其删除
' ?7 g: Y2 |# ~( e# |Set dbs = work.CreateDatabase(dbsname, _ 1 Z. f& H' K1 W# s( y* i1 W
dbLangGeneral)
8 @9 T8 K% C' OEnd If
. Q+ Y* o$ _/ x* |Set tdfNew = dbs.CreateTableDef 6 K; m; y8 R6 s% \% X
(“电气 _材料明细表”) + o8 @! P5 e" f' l. f9 D
‘建立一个名为电气材料明细表的表
- ?0 \' A# @* ~ |" k$ U' aRowNum = 0
1 a# W5 K, J4 d& s7 M& p2 EDim Header As Boolean
3 ?5 j1 ^- V9 J4 B) t uHeader = False , G1 r3 D. Q7 |* b; a& c1 A" S. G! ^# e
For Each elem In ThisDrawing.ModelSpace * H* e5 I7 f# l
‘在CAD模型空间,查找所有图形对象 ! l9 D* R k0 A J0 S
With elem % X4 w6 s8 ^4 ]- H( I
If StrComp(.EntityName,_ 6 P1 D, u/ P" N' O
“AcDbBlockReference”, 1) = 0 Then ' U8 R4 L9 S0 t, c" r
If .HasAttributes Then
9 \9 v' q3 H1 V( qarray1 = .GetAttributes S7 ^4 X6 `& r. ?
array2 = .GetConstantAttributes 6 |7 R7 m5 P, B- ?! m7 r
‘设置array1指向图形对象的属性
$ j6 w. {# H" g‘设置array2指向图形对象的固定属性
' k8 R. \+ N2 n( nFor Count = LBound(array2) To _ ' N6 j4 a! @( y, h* }* H, F
UBound(array2) ' j" z3 n9 H T. j/ P' A
If Header = False Then 3 w8 V7 g% a U% G6 i
If StrComp(array2(Count).EntityName, _ / Y# x( a8 E( J/ d& L! a& u7 c
“AcDbAttributeDefinition”, 1) = 0 Then
6 g: z( V, \. j5 \# StdfNew.Fields.AppendtdfNew._ + T6 T6 G& X( L; |8 Z
CreateField(array2(Count).TagString, dbText)
/ l' n7 k4 M+ Q+ A1 uEnd If
! \! b8 j- ]6 m9 Q% V‘读出属性值读出,作为Access数据库表的标题
( _# m7 Q: C" M; sEnd If
% l- k2 v/ I" v$ i- x+ GNext Count / B1 e: b; w5 z
For Count = LBound(array1) To _ 0 q: I; _4 p7 w# _( S$ d8 @
UBound(array1) 9 b* m' v+ ]( l0 P% i! h* C
If Header = False Then
+ q* M0 Y& `# ]4 O$ GIf StrComp(array1(Count).EntityName, _
. {* g& f) R1 R“AcDbAttribute”, 1) = 0 Then ! b3 M/ J' o& @, ]. A
tdfNew.Fields.Append tdfNew. _
1 \+ C; A f/ A2 ACreateField(array1(Count).TagString, dbText)
1 T# N( l- ?( a0 D1 h$ {- j4 UEnd If
0 K, e5 W" [) w1 H) Q0 P4 p+ UEnd If - V4 @' N9 \2 x) J6 f# ^
Next Count 1 ~$ U, l7 a$ Y. Q
If Header = False Then / H9 K8 ?( J. w" i* S
dbs.TableDefs.Append tdfNew " j6 w' g; ^5 m: f! B
Set rs = dbs.OpenRecordset ! N. E8 `* X0 R* m
(“电气材料 _明细表”, dbOpenTable) ‘打开记录
9 e& n( F* R9 f$ B, T2 ]! Y* ?" nEnd If 5 X% u' l$ h0 x+ a( L" I/ d. O; ]0 y" m
RowNum = RowNum + 1
& ^, k) J9 }+ r( m' y! s. trs.AddNew ‘增加一笔新记录 ) W' _* Q. n; |! F* q3 Y
For Count = LBound(array2) _ ' W( M! W' m4 ]( {* }$ f7 g
To UBound(array2)
' d. G% M; r0 U6 W& p/ M" m% ]7 srs(Count).Value = array2(Count).TextString 0 ~! S1 y* a* f" Q9 K# a& j
Next Count ‘读固定属性值
2 ?' |, W. z0 |& Q! L' @For Count = LBound(array1) To _
% ?3 a# \/ Q) O$ ]UBound(array1) 7 I# _0 y0 _6 W: s- i
rs(UBound(array2) + Count + 1).Value = _
; E- {' [) F f& harray1(Count).TextString 6 g4 ]! e( c: |4 @+ d+ q8 a
Next Count ‘读输入属性值
4 ]3 y9 S2 B7 m( Zrs.Update ‘增加新记录修改结束
. f( f8 c; l& c, f+ VHeader = True
g( E( g1 O: p; N a0 tEnd If
) o7 [" Q1 {5 D& n& y8 b1 c7 nEnd If , ?$ G' G, d. k6 Z% d$ i1 p& e
End With
4 t4 D7 M' `) H0 Y8 ?# i1 GNext elem 3 z/ d6 I8 d7 w" G& O
rs. Close ‘关闭记录,释放资源
1 C" J; _9 Z3 s+ d5 sdbs.Close ‘关闭数据库,释放资源 : y5 Y! x" o' h/ [
End Sub |
|