思路是将SW的BOM表导入到EXCEL,然后将EXCEL的数据(零件名+数量)写入到字典,然后通过文件名来匹配到字典里存的数据(数量)写入到零件的数量属性。其中提示请输入数据时需要粘帖数据进来。Myr = 500 '需人工设定。欢迎大家进行补充、使程序更智能。 ( u4 _3 _0 i4 I; l! F! J5 v& ]( u 9 {, W6 V) h) X" i; e7 n# oSub main()( S% K3 }/ Y# `1 ` E5 w '打开EXCEL表格开始1 X0 p/ e9 j. Q, i Dim ExcelSheet As Object : j. u+ p3 D8 Q/ p) G( qSet ExcelSheet = CreateObject("Excel.Sheet") 2 U* w1 F' r9 X# u3 B2 @# VExcelSheet.Application.Visible = True ( o! U8 K. ^' E" d$ N) y s" z* l'结束2 W8 M) s: M/ O) k8 X+ v% ^
# g/ p6 R8 z0 T: j. l'填入数据开始 9 V' V9 v# o5 ^# `Dim d3 m3 X( ~2 Z* ~/ D9 K4 S8 i1 x( j4 v Set d = CreateObject("Scripting.Dictionary") ) O, N5 e" {$ t y$ fMsgBox "请输入数据") }7 ?9 r$ e0 o: S7 [) A '结束 # v/ D4 K+ B, `! f - E0 ^( x5 h/ J; ]$ y" H/ ~'数据写入字典开始* y) m) y& X) ]5 | f Dim Myr& - I3 u# T: x+ I' K9 XMyr = 500 '需人工设定0 i# X$ E# e9 b2 \ For i = 1 To Myr+ \$ a& L. k N _/ F d(ExcelSheet.Application.Cells(i, 1).Value) = ExcelSheet.Application.Cells(i, 2).Value" Y* Z5 h6 O3 H2 V4 L Next 5 F8 c3 Z/ ]+ U( u7 ?! U'结束( w/ v, P& l' H0 K! E
* G8 t; O$ f- t: [5 n'将字典数据逐个写入到零件开始" z! p' a9 L+ W: o5 h9 b Dim swApp As Object & ^' Q& D G# tDim Part As Object ( @' x9 K" u0 u2 v. A# v" ?Dim longstatus As Long, longwarnings As Long + m$ Q0 Y& J3 H( t( X" B! I5 {Dim myPath$, myFile$. [) p' h" B8 G; f% F! D4 `6 W 1 b/ e$ T e! \ Set swApp = _ % _- }) x: E& O2 JApplication.SldWorks. y% Q% E. q1 R( ^- s1 }& O0 C: m myPath = "C:\Users\Administrator\Desktop\1\" '..........................重点:把文件路径定义给变量/ G/ D. x% d1 d. [5 d myFile = Dir(myPath & "*.sldprt") '依次找寻指定路径中的*.文件 5 o. s- B5 b- s. L3 B. w$ GDo While myFile <> ""5 E' L7 l1 H l; a Set Part = swApp.OpenDoc6(myPath & myFile, 1, 0, "", longstatus, longwarnings)5 B+ K; F' f& A1 ?& E, r
w$ K, i% G3 P- c'单个零件写入数据开始 1 w* n( d% ]" _ s'Dim swApp As Object0 F# K i, Q1 O4 R, x7 ^" M8 @( h1 i0 g Dim c As String 3 u1 d. T5 Z. w3 |: }Set swApp = Application.SldWorks ( Q8 a% O( y9 o- h9 F ^5 USet Part = swApp.ActiveDoc 2 H; {. d1 _( i% z4 h% ?c = swApp.ActiveDoc.GetTitle() '零件名4 E) t6 {" a0 s5 }4 x blnretval = Part.AddCustomInfo3("", "数量", swCustomInfoText, d.Item(c))5 ?2 T% X) o& I '单个零件写入数据结束 4 [. ~* ^+ W7 F0 ~( `4 s) E/ V# v$ `" t( a+ S0 L5 Q6 U) A Part.Save& J; ^2 e* V7 t9 y swApp.CloseDoc myPath & myFile ; Z( J' C6 Y) v1 wmyFile = Dir '找寻下一个*.文件 : g! n6 o w$ I. h: V4 zLoop& R' `# x9 \" C7 E& B, i, u '将字典数据逐个写入到零件结束 , E( K3 r7 }& FEnd Sub % Y% }7 j6 e& c) e |