思路是将SW的BOM表导入到EXCEL,然后将EXCEL的数据(零件名+数量)写入到字典,然后通过文件名来匹配到字典里存的数据(数量)写入到零件的数量属性。其中提示请输入数据时需要粘帖数据进来。Myr = 500 '需人工设定。欢迎大家进行补充、使程序更智能。
) R, }0 o5 B' b7 ]
+ F4 d$ r. _0 Q! qSub main()
. k" P6 b% j, U'打开EXCEL表格开始
" z# E6 S% B& n, i2 {Dim ExcelSheet As Object% |! B. W' N! U3 S; D
Set ExcelSheet = CreateObject("Excel.Sheet")
! C0 P- H. X- OExcelSheet.Application.Visible = True
$ _1 V" o0 v; T% S1 j9 f& D, W8 j'结束) T( t& q+ `7 p& j& v+ w; Q5 `
$ k! {% _5 Y# S ?* n) \% ?5 x'填入数据开始
5 Z$ y( e5 Q, p2 x6 p/ d- ^3 gDim d
) ^/ S$ H; \' |( LSet d = CreateObject("Scripting.Dictionary")
& ]6 y" W; l4 V( P2 IMsgBox "请输入数据"
0 y/ f$ |. ^5 Y& J5 @5 P& Q; J' M# D'结束
; I6 h( _# B+ T5 c# o0 ~) s( C5 @" n) V! U
'数据写入字典开始3 u% {& V. Z# U% O; ^
Dim Myr&: a2 @1 I2 W& r/ n
Myr = 500 '需人工设定5 T- G3 a% I r, \1 V0 w2 @7 y
For i = 1 To Myr
% x# E% c0 C. l; M! t7 \( ud(ExcelSheet.Application.Cells(i, 1).Value) = ExcelSheet.Application.Cells(i, 2).Value; j$ a* T; v0 N6 b
Next# u5 A7 {% x) b7 j4 u; a3 M
'结束
9 s, ^% Y* C. X, s. x" o, r( ~% O" _7 J
'将字典数据逐个写入到零件开始( y- u* ?9 u$ h0 | g( B2 U
Dim swApp As Object
+ E0 w6 w& w# ^Dim Part As Object" ~% X+ Q3 D8 J t8 V
Dim longstatus As Long, longwarnings As Long
, i8 t8 a5 ^2 TDim myPath$, myFile$; P: _, a# W8 s: V
( s# z3 w9 w6 v! i/ T) @
Set swApp = _
) z1 G5 n3 e+ @4 E6 R1 |/ {7 z* ~Application.SldWorks8 H( R+ y6 }- w5 u4 S# |, L1 h
myPath = "C:\Users\Administrator\Desktop\1\" '..........................重点:把文件路径定义给变量/ Q5 r( k5 g, U, R+ d
myFile = Dir(myPath & "*.sldprt") '依次找寻指定路径中的*.文件
$ `- r6 q: h2 O, `4 _Do While myFile <> ""
/ n" g2 ]% K# j4 k9 Q YSet Part = swApp.OpenDoc6(myPath & myFile, 1, 0, "", longstatus, longwarnings)) N- x# h) e" @' Q8 m6 u; K
. @' b# r7 g* x {4 B# \! g '单个零件写入数据开始6 r, n: X! o" Q( E' ?/ Z5 Y
'Dim swApp As Object8 C' z+ u8 q; h; t6 C
Dim c As String3 }* g' O, L5 I; M' @
Set swApp = Application.SldWorks
1 v& ` J* ^0 }, u/ BSet Part = swApp.ActiveDoc
. T9 G+ q& S. u+ s; b8 f! vc = swApp.ActiveDoc.GetTitle() '零件名# c( ~" O. A4 K% T
blnretval = Part.AddCustomInfo3("", "数量", swCustomInfoText, d.Item(c))0 i1 T3 K, _2 g8 B# i# W% {4 }, L0 X
'单个零件写入数据结束1 A' |- K$ {6 X! q7 K9 u* D, J7 H: u
6 K2 ~3 e8 Q2 ZPart.Save
" T8 [/ f. E6 E( _) jswApp.CloseDoc myPath & myFile! |: j* N# H' n2 N8 s
myFile = Dir '找寻下一个*.文件
) J; W7 m) y. z& S" }/ hLoop4 l0 `5 D- d3 O8 H3 k: K: @
'将字典数据逐个写入到零件结束
' d; w* y/ B9 `0 j1 q7 NEnd Sub `* ^7 Z8 b$ e2 m0 B5 Q, k+ h
|