1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| Dim swApp As SldWorks.SldWorks = Nothing
Dim swModel As SldWorks.ModelDoc2 = Nothing
Dim swModelExt As SldWorks.ModelDocExtension = Nothing
Dim swBOMAnnotation As SldWorks.BomTableAnnotation = Nothing
Dim swBOMFeature As SldWorks.BomFeature = Nothing
Dim swTableAnno As SldWorks.TableAnnotation = Nothing
Dim ModelName As String = ""
Dim bExcelResult As Boolean = False
Const BOMTemplate As String = "C:\YourBOMTemplatePath\MyBOMTemplate.sldbomtbt"
Const xlSavePath As String = "C:\YourExcelPathHere\"
swApp = GetObject(, "SldWorks.Application")
If swApp IsNot Nothing Then
swModel = swApp.ActiveDoc
Dim DocType As Integer = swModel.GetType()
If Not DocType = 2 Then
MessageBox.Show("You Must Run This Macro Inside A SolidWorks Assembly!", _
"SolidWorks Document Type Error", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Application.Exit()
End If
If DocType = 2 Then
ModelName = swModel.GetPathName()
ModelName = Path.GetFileNameWithoutExtension(ModelName)
swModelExt = swModel.Extension
swBOMAnnotation = swModelExt.InsertBomTable2(BOMTemplate, -1.0, 0.0, _
swBomType_e.swBomType_PartsOnly, _
"000-000-000", False)
swBOMFeature = swBOMAnnotation.BomFeature
swTableAnno = swBOMAnnotation
Dim xlSaveName As String = xlSavePath + ModelName + ".xls"
bExcelResult = swTableAnno.SaveAsText(xlSaveName, vbTab)
swBOMFeature.GetFeature.Select2(False, 0)
swModelExt.DeleteSelection2(0)
End If
End If |
Partager