思路是將SW的BOM表導入到EXCEL,然后將EXCEL的數(shù)據(jù)(零件名+數(shù)量)寫入到字典,然后通過文件名來匹配到字典里存的數(shù)據(jù)(數(shù)量)寫入到零件的數(shù)量屬性。其中提示請輸入數(shù)據(jù)時需要粘帖數(shù)據(jù)進來。Myr = 500 '需人工設定。歡迎大家進行補充、使程序更智能。( ]' Q l5 K+ p& Q- g9 z
) m; Q! {0 i, X3 y/ s: f5 X3 uSub main()
|6 k# ]- b1 R, r( F/ c9 ]# F'打開EXCEL表格開始
6 r; }) _# i) z. T5 v! F& aDim ExcelSheet As Object7 m) D; i% s/ k! I, }, |6 x. q
Set ExcelSheet = CreateObject("Excel.Sheet")
' }9 Y; r: f2 }; \ExcelSheet.Application.Visible = True0 E. h! w6 @. L- j# D
'結束
! i1 K$ s1 t% [) J( i- w. o: e* t, D3 c
'填入數(shù)據(jù)開始
1 i/ a1 s! Z. t9 O6 P. E9 p3 d9 T- n4 pDim d
+ L: r$ ~3 f( L3 U9 L% R* CSet d = CreateObject("Scripting.Dictionary")5 N) {* D" h3 F) e- a: n
MsgBox "請輸入數(shù)據(jù)"5 U6 J" s6 z! ]% T
'結束
0 O& \+ [/ s5 U& A
- J% H7 k( G5 f" h( l0 z'數(shù)據(jù)寫入字典開始( f3 N9 D2 g* S. U! X: N) L
Dim Myr&/ g% M, }5 m2 S! w4 X) U7 X% ~1 z
Myr = 500 '需人工設定% a, }! f( z* ~) f7 v
For i = 1 To Myr
5 n$ i1 O g9 |& qd(ExcelSheet.Application.Cells(i, 1).Value) = ExcelSheet.Application.Cells(i, 2).Value
# @. S# l# H6 V- UNext+ Z# X8 I2 P: s, L' c$ A& G
'結束
; Y1 q8 w3 U$ `% H5 w
7 S3 \2 w( I& s9 x9 [: y3 I7 \: S'將字典數(shù)據(jù)逐個寫入到零件開始5 j5 e f! [' m$ }2 `. @
Dim swApp As Object
$ s: T: A/ n( H" Y* \: ?9 ~2 SDim Part As Object3 y# a- W3 Y& M! w- E( T
Dim longstatus As Long, longwarnings As Long' Z% }, i A& }7 p) f
Dim myPath$, myFile$6 y( q7 S0 M, h
( B/ J2 i8 |) H- o# tSet swApp = _
* V8 ?9 e/ v i% A$ GApplication.SldWorks
/ g* i5 {9 }9 J! ~' d8 U( I. m! o5 HmyPath = "C:\Users\Administrator\Desktop\1\" '..........................重點:把文件路徑定義給變量: q& {# j6 N8 |2 {
myFile = Dir(myPath & "*.sldprt") '依次找尋指定路徑中的*.文件
+ d% f0 X' x. b3 E: U4 [( d9 EDo While myFile <> ""
6 `5 D k7 l5 f( r% ySet Part = swApp.OpenDoc6(myPath & myFile, 1, 0, "", longstatus, longwarnings)" n/ Y, m. [% W/ X6 ~3 x
& M. n* I2 v3 Z% X- \ '單個零件寫入數(shù)據(jù)開始
0 ?* s) j: y' ~/ J: g% R0 F7 e: S'Dim swApp As Object
# Q. w) e1 `3 d1 r4 {: SDim c As String+ w; q q4 A% L
Set swApp = Application.SldWorks3 r" }' \" y- q! F- v/ H( S
Set Part = swApp.ActiveDoc9 Z6 D- ]$ q b4 E8 u
c = swApp.ActiveDoc.GetTitle() '零件名. a: ~5 |* J' ]! B/ D
blnretval = Part.AddCustomInfo3("", "數(shù)量", swCustomInfoText, d.Item(c))2 o4 ]& k: z K
'單個零件寫入數(shù)據(jù)結束' \% J9 |/ I9 c- t) o7 x, j8 i
0 J' X _' S) ZPart.Save
3 Q. ~; Q/ l" yswApp.CloseDoc myPath & myFile
- b/ l" ]3 @& {8 BmyFile = Dir '找尋下一個*.文件
: @( X, D4 N; X; [0 b( {1 oLoop
5 O" ^$ s4 n! f. s& v& p* Q4 ['將字典數(shù)據(jù)逐個寫入到零件結束
9 y8 H ^% X: D; aEnd Sub; o O1 n9 J8 w# R( Q
|