思路是将SW的BOM表导入到EXCEL,然后将EXCEL的数据(零件名+数量)写入到字典,然后通过文件名来匹配到字典里存的数据(数量)写入到零件的数量属性。其中提示请输入数据时需要粘帖数据进来。Myr = 500 '需人工设定。欢迎大家进行补充、使程序更智能。 ' N$ V) B, @5 i2 i / K- A) D4 [# wSub main() # \2 ?0 N% ?8 [' K1 F; Z# |0 n'打开EXCEL表格开始3 u) u/ ^3 a1 c* g0 u- } Dim ExcelSheet As Object 2 E3 U8 A9 P6 H( W r- _Set ExcelSheet = CreateObject("Excel.Sheet")) e0 q3 o7 l4 t3 g0 ?' I' { ExcelSheet.Application.Visible = True; Z; }' z8 X- D$ M; w( K '结束 , O! F) q: n# Y+ X 2 u* E2 \; ]4 y- E( E& ~6 |- R'填入数据开始$ k* b* c1 i6 T1 ~$ G3 J8 X Dim d # T/ t. i! ^! E" \+ sSet d = CreateObject("Scripting.Dictionary")' T7 b9 K5 U' w' j MsgBox "请输入数据" u4 `+ L" c# r# T! {' s'结束 . f. v. F8 ?( e: a+ d' E& i6 t; E/ x: Z4 j5 v '数据写入字典开始# T! }% n ~+ z# a3 o5 i$ s4 u Dim Myr& 7 L! G) D% J- q5 fMyr = 500 '需人工设定% H( X; P7 E. Y/ ~2 V& ` For i = 1 To Myr6 N# G! \6 c& B d(ExcelSheet.Application.Cells(i, 1).Value) = ExcelSheet.Application.Cells(i, 2).Value! d( o8 v" ?! | Next0 c/ F7 }5 J! x: C: b, d '结束% z0 W; \" ]+ H* j- {( g3 q) T ) \# B& H7 a! v& x5 [; y" @, h '将字典数据逐个写入到零件开始 3 f0 G6 {& p3 g+ s" [2 F0 n9 [# V1 oDim swApp As Object: i3 l! u& z* l Dim Part As Object) J9 I, ?- X: Q, H; v0 n$ D Dim longstatus As Long, longwarnings As Long+ L; K! k0 g0 A& A1 `9 | Dim myPath$, myFile$ 2 O; X: {) n) n 9 h: p3 ?2 k8 y% r) BSet swApp = _% Z, N5 n1 i! ~9 X) H Application.SldWorks ) I2 a( x6 k- _% R* h* WmyPath = "C:\Users\Administrator\Desktop\1\" '..........................重点:把文件路径定义给变量 5 P2 }& U, o- o& I. P/ I+ ~( tmyFile = Dir(myPath & "*.sldprt") '依次找寻指定路径中的*.文件 ; a b) M+ e( h! @) F3 vDo While myFile <> "" 6 _/ M0 k2 Z# I; a5 I3 F- ASet Part = swApp.OpenDoc6(myPath & myFile, 1, 0, "", longstatus, longwarnings) 8 v3 W% |& ~. a+ ? f- g & Z0 H7 Z) I! G2 m1 d$ {'单个零件写入数据开始 % b) g- j! P# M'Dim swApp As Object / J7 ~8 K# e) T; B1 ^/ ~Dim c As String9 J6 V0 Q) o" [& |: e Set swApp = Application.SldWorks " d6 B/ K8 o5 m- n/ G3 F0 N* ?! TSet Part = swApp.ActiveDoc& v1 @+ V0 e' t5 W, y5 i c = swApp.ActiveDoc.GetTitle() '零件名 3 `& j' k- [+ jblnretval = Part.AddCustomInfo3("", "数量", swCustomInfoText, d.Item(c)) 0 W9 {* }, H; |% r+ f1 w'单个零件写入数据结束* c C# q$ p5 n7 m1 x$ d
2 H( ~: r" K ~# dPart.Save ! w8 h, v6 i! p+ u0 S; rswApp.CloseDoc myPath & myFile ' U( V$ X& O/ E: v7 [7 c3 P8 MmyFile = Dir '找寻下一个*.文件 2 a5 F: r- X6 o: C" {% qLoop+ k& ]% |) ]. n- W8 b) y '将字典数据逐个写入到零件结束 9 K( o/ u% l7 ] n. n* o. ~- kEnd Sub* P0 J1 G) d/ K: L# {' T4 r+ ]
|