CAD设计论坛

 找回密码
 立即注册
论坛新手常用操作帮助系统等待验证的用户请看获取社区币方法的说明新注册会员必读(必修)
查看: 1642|回复: 2

[求助] 高手请进来看看这段 VBA代码

[复制链接]
发表于 2007-6-3 19:35 | 显示全部楼层 |阅读模式
这段代码 我找到时 说明是可以将AUTOCAD中的属性块中的属性提取到Excel中) m5 U- {2 h+ r# z, g
我用的是AUTOCAD2007 打开VBA复制进去 然后工具-引用,把Microsoft  Excel勾选上
5 |' I" n! E8 t; c! B: G然后编译   光标停留在“mspace As Object”这句上
" ^  s+ r: u" K, H% i编译报错  “成员已经存在于本对象模块派生出的对象模块中”8 [& k* J% o2 k1 b7 u6 Q
然后小弟查了很久 也不知道 对不对 把mspace改成了myspace
' x3 r3 B2 W8 o+ e6 T: U再编译就没有报错 通过了
$ T3 @1 v5 d( }. z但是运行宏的时候 又报错“运行时错误429,ActiveX部件不能创建对象”
" q9 m* M3 n0 M- a* f请各位帮忙看一下 或者 高手可以指点一下小弟$ \6 C8 j% m! F' V
感激万分( d7 w/ v8 z) u/ t+ x4 c
* p" ^" H7 B) |6 ?3 G6 o

& z' g, I* r, g3 xPublic acad As Object
  T: l! ^" w* M" i- XPublic mspace As Object
3 L0 g; m0 U3 e3 e7 iPublic excel As Object
: I" b0 p1 l" r1 t' x$ J8 LPublic AcadRunning As Integer
, h" Y" c# p6 x, oPublic excelSheet As Object. m0 |" V1 f8 R( b) d, [' z" A$ a
Sub Extract()
$ Y$ V6 |6 G& c: L    Dim sheet As Object. L6 I$ h& i) M! i- a
    Dim shapes As Object7 a5 ?3 h" o" M+ {) d" z7 S
    Dim elem As Object; |; o$ k5 V+ }! y' o
    Dim excel As Object1 |' X8 H0 [4 j2 a' T
    Dim Max As Integer$ k' I& P+ E! |
    Dim Min As Integer7 s0 [0 _/ M. S' [, C
    Dim NoOfIndices As Integer
; Y7 h. e" [/ s5 E6 m    Dim excelSheet As Object8 d+ R. w9 ^& F# _1 H$ e
    Dim RowNum As Integer# K* e" c& S% h% o- h
    Dim Array1 As Variant, Array2 As Variant
5 \" ?1 i! \+ G' W. I( Y! ~    Dim Count As Integer  U3 R1 V, e" s. R- w! c* q( a5 a

3 K6 g: T; R9 v2 b4 C( F2 \( Y1 h( d% E% m8 c1 b% s
" c9 j1 s+ m4 i% I  [
    Set excel = GetObject(, "Excel.Application"): T3 M$ |! K; W7 ~
Set excelSheet = excel.Worksheets("sheet1")  J, i4 ?3 j9 @( f4 U
     Dim Sh As Object, rngStart As Range/ @8 q. o  T" C  k
     If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub2 Y) U) U' S# v
     Set Sh1 = ExcelSheet1' |4 _- `8 P- {& i
Set rngStart = Sh1.Range("A1")( j" C0 [& g3 A
    With rngStart.Rows(1)2 r4 @% Z+ l3 ]; ?1 y1 q6 G
End With: J4 t1 C4 }  @( }$ R1 P
    Set acad = Nothing
( [! P9 W* a) G1 B  g    On Error Resume Next
2 G$ n( W$ j- b, U7 S  C7 Z9 Q5 ]    Set acad = GetObject(, "AutoCAD.Application")
! |, V, v- x# l9 D    If Err <> 0 Then% @% L  m/ D  R1 d* f# |$ r6 N) Q
    Set acad = CreateObject("AutoCAD.Application")5 i1 p, w  r( T$ r6 R8 j
    MsgBox "请打开 AutoCAD 图形文件!"
8 m' y/ `+ y2 t) \; J    Exit Sub
$ H! l. b4 `3 O/ S    End If
6 ^6 f. w" u" C) F4 n! L! h3 j5 W3 O+ r" ^' O
    Set doc = acad.ActiveDocument
! M/ ]; F+ F& H8 W" e! \& l* [    Set mspace = doc.ModelSpace
6 w( K6 u) O. L/ S4 Q; `+ s    RowNum = 1
8 |- Z$ A5 l4 R' G; X, o' e    Dim Header As Boolean
  D5 ~. o7 l6 b5 U) Y- d. v" [    Header = False
6 `3 x: c( n# o9 R! H% [: p    For Each elem In mspace# j. M* T: P- {( B
      With elem
1 e  J0 q( v2 C- A) t        If StrComp(.EntityName, "AcDbBlockReference", 1) = 0 Then) W# i) ?! x$ B4 W5 y2 i: `0 @
           If .HasAttributes Then
3 d, W  P; M% _( K3 @+ ^% y              Array1 = .GetAttributes5 o% R% f$ L7 m5 B9 n: `7 F
              Array2 = .GetConstantAttributes( O6 @/ V" e& x* w9 z8 ~; V
            For Count = LBound(Array1) To UBound(Array1)$ ]( v2 i+ n7 Q/ y7 ]# ~) o
               If Header = False Then
4 K- `) r2 e1 L* |! x) Y% e/ J                 If StrComp(Array1(Count).EntityName, "AcDbAttribute", 1) = 0 Then
2 N0 V+ |2 ~1 q% _  H                  excelSheet.Cells(RowNum, Count + 1).Value = Array1(Count).TagString4 q; N% d* O! q  {% \
                 End If3 _0 w$ D; r  U
               End If. E& l0 }) O& t9 m9 V
            Next Count
1 C7 w/ x7 r: l6 U0 q            
, V/ n" N% I, g" m9 t7 ]/ f            For Count = LBound(Array2) To UBound(Array2)
/ @+ A: m1 K2 s3 W               If Header = False Then
( A( a2 J% I$ r8 |& Z6 e                 If StrComp(Array2(Count).EntityName, "AcDbAttributeDefinition", 1) = 0 Then
7 l. [  G7 r6 b' w5 q                  excelSheet.Cells(RowNum, UBound(Array1) + 1 + Count + 1).Value = Array2(Count).TagString1 Z% A& I1 b% _3 m  R
                 End If: S& b: y% X3 O9 \; F) [
               End If
8 m6 _$ u4 u7 B1 G% t6 [2 r# v            Next Count
9 {) @8 W5 e4 j" e            : _1 n7 T3 Y. N1 {
              RowNum = RowNum + 1: v$ X  ^5 P# e) f' X- F
            For Count = LBound(Array1) To UBound(Array1)$ i' z. d0 f$ d8 W; o  T8 O) E
               excelSheet.Cells(RowNum, Count + 1).Value = Array1(Count).TextString' m9 k  Y' ~$ l# [
            Next Count
# \) F% |1 x: f( Y7 c4 U            * ~* u' G+ g# T/ h/ ^  e/ f
            For Count = LBound(Array2) To UBound(Array2)
$ G7 X: _' s  j+ A  o               excelSheet.Cells(RowNum, UBound(Array1) + 1 + Count + 1).Value = Array2(Count).TextString. q0 K; u2 G8 j3 g% a) N( f% p
            Next Count
7 w7 u( H& R) r1 `- K& o# P2 t            - R$ f/ {! K7 w4 v  I/ P
            Header = True0 p+ N* g/ L$ Q$ H3 F( A- L
            End If
% `% n6 y& e+ i1 i/ b          End If
2 R8 z4 r) q) r7 |) U  W: j      End With
0 `  Z' B, y& A- }: z    Next elem
: l6 |5 }% ^) s8 B) P+ k/ c# {    NumberOfAttributes = RowNum - 1
5 c* ]2 `, h- Z4 _: I1 f. i    If NumberOfAttributes > 0 Then! r9 v# `8 X- w" p& d' ?/ p
      Worksheets("属性取出").Range("A1").Sort _
. C4 ^. [8 p; P7 P      key1:=Worksheets("属性取出").Columns("A"), _
; a: P: m" M: q! }: T4 I0 J      Header:=xlGuess
3 a) z$ S7 ?+ z, _3 b  m" _    Else
3 E1 x6 l  E; C/ ^7 `' t5 m5 e      MsgBox "无法提出图形文件中的属性或此图形文件中无任何属性!"( }& Z$ P0 r  g( I  N
    End If$ n- w, M' ^* Q
   
; N4 q! d* P+ r& g    Set currentcell = Range("A2")
  t) a* \$ d4 e$ k8 N4 R5 w3 b* n+ q    Do While Not IsEmpty(currentcell)9 q2 m3 c! t% K$ _" B, V; C
        Set nextCell = currentcell.Offset(1, 0); h: R! g; A0 ]8 i
        If nextCell.Value = currentcell.Value Then
6 X# h, t  ]. Y* A/ K& F4 m* {5 H            Set TCell = currentcell.Offset(1, 3)' G7 U! T& ]1 f$ \( `, A
            TCell.Value = TCell.Value + 1
3 g5 U$ G! x( k            currentcell.EntireRow.Delete
- ~$ f! M0 Q5 j2 |        End If2 C! F1 A  [3 p! T" ^
        Set currentcell = nextCell; i6 y* R$ g1 j2 _/ |. b1 \
    Loop
$ [: z' K  x+ R
: i7 `  c6 |# l$ v) Q: ]: y    / l0 ]# W2 N) }$ X; M$ X- ~! g& v: ^
    Set acad = Nothing5 M, q7 {) ^& G
End Sub
发表于 2007-6-3 21:28 | 显示全部楼层
发表于 2007-6-4 20:46 | 显示全部楼层
这个真的难说的6 t/ n& ^3 ]4 R/ }& w  m
想当年用EXCEL宏的时候也经常出错
  E" [: C" e9 Y* G自己慢慢的去调试
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于|免责|隐私|版权|广告|联系|手机版|CAD设计论坛

GMT+8, 2026-7-16 14:41

CAD设计论坛,为工程师增加动力。

© 2005-2026 askcad.com. All rights reserved.

快速回复 返回顶部 返回列表