I'm trying to write a macro that takes an open part/assembly, inserts it into a new assembly, generates a bill of materials for the new assembly, and then exports the BOM as a .xls. At the minute I am stuck on getting the BOM to insert and keep getting the object or with block variable not set at the Set swBOMAnnotation line and for the life of me can't find it.
Code is as follows
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As AssemblyDoc
Dim swComponent As SldWorks.Component2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swBOMAnnotation As SldWorks.BomTableAnnotation
Dim swBOMFeature As SldWorks.BomFeature
Dim boolstatus As Boolean
Dim BomType As Long
Dim Configuration As String
Dim TemplateName As String
Dim nErrors As Long
Dim nWarnings As Long
Dim swBOMTable As Object
Dim swConfig As SldWorks.Configuration
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
swApp.SendMsgToUser "Open a document to export BOM"
Exit Sub
End If
modPath = swModel.GetTitle
Debug.Print "path name equals " & modPath; "."
swApp.NewAssembly
Set swAssy = swApp.ActiveDoc
Set swComponent = swAssy.AddComponent2(modPath, 0, 0, 0)
swAssy.ForceRebuild
TemplateName = "C:\TAW_CE\Templates\TAWEXPORTBOM.sldbomtbt"
BomType = swBomType_Indented
Set swConfig = swAssy.GetActiveConfiguration
Configuration = swConfig.Name
Set swBOMAnnotation = swModelDocExt.InsertBomTable3(TemplateName, 0, 1, BomType, Configuration, False, swNumberingType_Detailed, True)
Set swBOMFeature = swBOMAnnotation.BomFeature
End Sub
SolidworksApi/macros