Option Explicit
Dim swApp As Object
Dim swPart As Object
Dim swView As Object
Dim swBOM As Object
Public Type BOM_Data
Rev As String
Item As String
Qty As String
Desc As String
Wgt As String
Matl As String
Spec1 As String
Spec2 As String
PartNo As String
End Type
Public LineItem() As BOM_Data
Sub main()
Dim ret As Variant, sTmp As String
Dim i As Integer, iItems As Integer
Dim sMisc(7) As String
Dim BomViewsModelName As String
Dim dataArray As Variant
'Attach to SolidWorks
On Error Resume Next
Set swApp = GetObject(, "SldWorks.Application")
If Err.Number <> 0 Then
MsgBox "Can not Find SldWorks.Application" & vbCrLf & _
"ErrNo: " & Err.Number & " ErrMsg: " & Err.Description _
, vbOKOnly, "Error in ExportBOM()"
Err.Clear
Exit Sub
End If
On Error GoTo 0
'Comment this out to debug
On Error GoTo ErrorEB
Set swPart = swApp.ActiveDoc
Set swView = swPart.GetFirstView 'This is actually the template
'Get the BOM
Set swBOM = swView.GetBomTable
'Find the BOM - must find the view that contains the BOM
Do While swBOM Is Nothing And Not swView Is Nothing
Set swView = swView.GetNextView
Set swBOM = swView.GetBomTable
Loop
If swBOM Is Nothing Then
MsgBox "Can NOT find a BOM on the current drawing!"
Exit Sub
End If
'Attach to the BOM
ret = swBOM.Attach2
If ret = False Then
MsgBox "Error Attaching to BOM"
Exit Sub
End If
'Put the BOM table in an array
iItems = swBOM.GetRowCount - 1
ReDim LineItem(iItems)
For i = 1 To iItems
LineItem(i).Rev = swBOM.GetEntryText(i, 0)
LineItem(i).Item = swBOM.GetEntryText(i, 1)
LineItem(i).Qty = swBOM.GetEntryText(i, 2)
LineItem(i).Desc = swBOM.GetEntryText(i, 3)
LineItem(i).Wgt = swBOM.GetEntryText(i, 4)
LineItem(i).Matl = swBOM.GetEntryText(i, 5)
LineItem(i).Spec1 = swBOM.GetEntryText(i, 6)
LineItem(i).Spec2 = swBOM.GetEntryText(i, 7)
LineItem(i).PartNo = swBOM.GetEntryText(i, 8)
Next i
'Detach from the BOM
swBOM.Detach
'Convert Linked Dimensions Here - Skipped
'Open the Excel File
'Use Workbooks.Open to open an existing file
Workbooks.Add
Workbooks.Application.Visible = True
'Write out the data
For i = 1 To iItems
Range("A" & i).FormulaR1C1 = LineItem(i).Rev
Range("B" & i).FormulaR1C1 = LineItem(i).Item
Range("C" & i).FormulaR1C1 = LineItem(i).Qty
Range("D" & i).FormulaR1C1 = LineItem(i).Desc
Range("E" & i).FormulaR1C1 = LineItem(i).Wgt
Range("F" & i).FormulaR1C1 = LineItem(i).Matl
Range("G" & i).FormulaR1C1 = LineItem(i).Spec1 & Space(2) & LineItem(i).Spec2
Range("H" & i).FormulaR1C1 = LineItem(i).PartNo
Next i
Range("A4").Select
'***************************************
'Rearrange and Print the Excel File Here
'***************************************
MsgBox "BOM Exported Successfully!"
GoTo CleanUp
ErrorEB:
MsgBox "Error in ExportBOM() Utility" & vbCrLf & Err.Description
Err.Clear
CleanUp:
Set swApp = Nothing
Set swPart = Nothing
Set swView = Nothing
Set swBOM = Nothing
End Sub