Sperman is correct, i missed out the sub routine, move the code that set the table layer to the subroutine should fix the issue
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swTable As SldWorks.TableAnnotation
Dim swLayerMgr As SldWorks.LayerMgr
Dim swLayer As SldWorks.Layer
Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long
Const ANCHOR_TYPE As Integer = swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopRight
Const BOM_TYPE As Integer = swBomType_e.swBomType_PartsOnly
Const TABLE_TEMPLATE_SIMPLE As String = “\vmisw2022\DataSolidWorks\ModelesVMI\Tables\Nomenclatures-Qte-CodeSAP.sldbomtbt”
Const TABLE_TEMPLATE_STD As String = “\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP.sldbomtbt”
'Const TABLE_TEMPLATE_BASE_FR As String = “\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_FR.sldbomtbt”
'Const TABLE_TEMPLATE_BASE_EN As String = “\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_EN.sldbomtbt”
'Const TABLE_TEMPLATE_BASE_AL As String = “\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_AL.sldbomtbt”
'Const TABLE_TEMPLATE_BASE_ES As String = “\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_ES.sldbomtbt”
Const INDENTED_NUMBERING_TYPE As Integer = swNumberingType_e.swNumberingType_Flat
Const DETAILED_CUT_LIST As Boolean = False
Const FOLLOW_ASSEMBLY_ORDER As Boolean = True
Const ALL_SHEETS As Boolean = False
Sub main()
Set swApp = Application.SldWorks
Set swDraw = swApp.ActiveDoc
'Get active document
Set swModel = swApp.ActiveDoc
'Display message and terminate if not drawing
If Not swModel.GetType() = swDocDRAWING Then
'If not drawing
MsgBox “Macro only work on drawing file.”, vbCritical, “ERROR”
End
End If
Set swDraw = swModel
'Création calque BOM_SIMPLE
boolstatus = swDraw.CreateLayer(“BOM_SIMPLE”, “Nomenclature Ss Description”, 255, swLineCONTINUOUS, swLW_THICK, True)
'Active calque BOM_SIMPLE
boolstatus = swDraw.SetCurrentLayer(“BOM_SIMPLE”)
'If ALL_SHEETS Then
'Dim vSheetNames As Variant
'vSheetNames = swDraw.GetSheetNames
'Dim activeSheetName As String
’ activeSheetName = swDraw.GetCurrentSheet().GetName
'Dim i As Integer
'For i = 0 To UBound(vSheetNames)
'Dim swSheet As SldWorks.sheet
’ Set swSheet = swDraw.sheet(CStr(vSheetNames(i)))
'InsertBomTable swDraw, swSheet
'Next
'swDraw.ActivateSheet activeSheetName
'Else
InsertBomTable swDraw, swDraw.GetCurrentSheet
'End If
'Set turn off layer visibility
Set swLayerMgr = swModel.GetLayerManager
Set swLayer = swLayerMgr.GetLayer(“BOM_SIMPLE”)
swLayer.Visible = False
Set swModel = Nothing
End Sub
Sub InsertBomTable(draw As SldWorks.DrawingDoc, sheet As SldWorks.sheet)
If False = draw.ActivateSheet(sheet.GetName()) Then
Err.Raise vbError, “”, "Failed to activate sheet " & sheet.GetName
End If
Dim vViews As Variant
vViews = sheet.GetViews
Dim swView As SldWorks.View
Set swView = vViews(0)
Dim swBomTableAnn As SldWorks.BomTableAnnotation
Set swBomTableAnn = swView.InsertBomTable4(True, 0, 0, ANCHOR_TYPE, BOM_TYPE, “”, TABLE_TEMPLATE_SIMPLE, False, INDENTED_NUMBERING_TYPE, DETAILED_CUT_LIST)
If Not swBomTableAnn Is Nothing Then
swBomTableAnn.BomFeature.FollowAssemblyOrder2 = FOLLOW_ASSEMBLY_ORDER
'Set BOM Layer
Set swTable = swBomTableAnn
swTable.GetAnnotation.Layer = “BOM_SIMPLE”
Else
Err.Raise vbError, “”, "Failed to insert BOM table into " & swView.Name
End If
End Sub