思路是将SW的BOM表导入到EXCEL,然后将EXCEL的数据(零件名+数量)写入到字典,然后通过文件名来匹配到字典里存的数据(数量)写入到零件的数量属性。其中提示请输入数据时需要粘帖数据进来。Myr = 500 '需人工设定。欢迎大家进行补充、使程序更智能。
+ j9 D5 q5 n6 V1 M1 [2 J2 K% K0 Q* U0 S1 W/ ^) D/ T
Sub main()& _5 v0 x* N1 `9 o
'打开EXCEL表格开始
% |" ^) Q; U, t+ y+ ]Dim ExcelSheet As Object5 [3 M/ X: D% E/ j8 N" R
Set ExcelSheet = CreateObject("Excel.Sheet")
% y( [( `& f4 \ExcelSheet.Application.Visible = True, y8 H+ \& M' j
'结束5 k: Z. A: M8 m) W
1 {- C8 V/ k# F' n5 M, A9 p* L'填入数据开始+ b' c3 O* P& Q$ T7 \, [5 N! q9 q. {
Dim d
) K; O1 m4 I% g8 `! f9 v) jSet d = CreateObject("Scripting.Dictionary")" \5 |" M6 a" p% W: U; E( f' f
MsgBox "请输入数据"! m. t# |! v/ W6 U r
'结束1 _$ D0 f8 H% t7 V9 S8 R
3 n: Q% Q8 v" d* a- b
'数据写入字典开始3 @. o7 Z+ a4 `0 t/ ~
Dim Myr&* W( D" k% E# _1 Y+ l: P- d
Myr = 500 '需人工设定
! h! o# {: Y2 R OFor i = 1 To Myr1 i& w2 O% h) k" S8 Z5 ~
d(ExcelSheet.Application.Cells(i, 1).Value) = ExcelSheet.Application.Cells(i, 2).Value
; R$ q9 d5 _2 b' R" A \& L' DNext; R- o; Z/ Q: b6 i' F7 q! R
'结束
! z4 f7 }; H( {1 N2 ~5 M: v' M
0 K: K7 @# J2 w2 e0 S! L'将字典数据逐个写入到零件开始
4 r3 C4 e) c, d" M: A. H$ k- X* CDim swApp As Object8 ~2 a5 ~6 [3 N' ]( H( e& C, u
Dim Part As Object+ u' B$ L0 p3 ]* j" z& K
Dim longstatus As Long, longwarnings As Long
! T: q4 y( A4 ~7 K$ M# YDim myPath$, myFile$8 X! T* S! V1 \+ k7 `
7 v! n& A3 [) m+ h; uSet swApp = _
4 w- r7 I. _ OApplication.SldWorks
3 X2 G4 w; Q" B. x8 PmyPath = "C:\Users\Administrator\Desktop\1\" '..........................重点:把文件路径定义给变量: t# C2 D: ~8 D; }* z
myFile = Dir(myPath & "*.sldprt") '依次找寻指定路径中的*.文件
2 y+ P$ f3 }# j: {" rDo While myFile <> ""
: B. X3 S( |1 z/ TSet Part = swApp.OpenDoc6(myPath & myFile, 1, 0, "", longstatus, longwarnings)
- W8 [7 ^. X" m6 i+ @
5 {8 P6 B" X" v '单个零件写入数据开始+ t+ F- r* [% v* r. [, m
'Dim swApp As Object
0 \8 B3 e9 F( L4 e7 S3 qDim c As String8 n" O$ s4 v2 T2 K" H4 y% [; K9 W
Set swApp = Application.SldWorks
$ T8 `, p4 I9 G7 p, B( zSet Part = swApp.ActiveDoc
% }& \ x5 h! _' p/ rc = swApp.ActiveDoc.GetTitle() '零件名' B" r6 M, D ]3 u2 [$ Y
blnretval = Part.AddCustomInfo3("", "数量", swCustomInfoText, d.Item(c))
3 ^9 b) J& J9 t! }7 _ '单个零件写入数据结束' I* R. k/ g \$ B
% V5 N0 }! t- v2 t, G9 F, b) i
Part.Save x4 f0 A3 a. h' d
swApp.CloseDoc myPath & myFile' w X, f8 [" F
myFile = Dir '找寻下一个*.文件
0 U0 p1 Y l$ l1 C! nLoop& w/ \* T6 s; N+ J0 r1 R1 ~
'将字典数据逐个写入到零件结束
7 w' C) ~5 p6 s# o7 `End Sub/ M8 I* t( z G' l) I8 E
|