Solidworks 2019 - Macro to automatically export a macro to Excel

Hello

I used a macro posted by another user.

I quickly have a bug and I admit that I am not a macro specialist. The program crashes with a Run-Tume error 91 (object Variable with block variable not set) in the Set statement swBOMFeature = swBOMAnnotation.BomFeature in bold

Could you tell me where I'm going wrong and how to solve my problem

thank you in advance

Sub main()

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
Dim wbk As Excel.Workbook
Dim sht As Excel.Worksheet

With xlApp
. Visible = True
Set wbk = . Workbooks.Add
Set sht = wbk. ActiveSheet
End With

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
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 TableTemplate As String

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension


TemplateName = "C:\Users\sal1chl\Desktop\test_nomenclature.sldbomtbt"

BomType = swBomType_TopLevelOnly

Set swBOMAnnotation = swModelDocExt.InsertBomTable3(TemplateName, 0, 0, BomType, Configuration, False, swNumberingType_Detailed, True)
Set swBOMFeature = swBOMAnnotation.BomFeature

swModel.ForceRebuild3 True

Dim NumCol As Long
Dim NumRow As Long
Dim I As Long
Sun J As Long

NumCol = swBOMAnnotation.ColumnCount
NumRow = swBOMAnnotation.RowCount

For I = 0 To NumRow
For J = 0 TB NumCol
Sht. Cells(I + 1, J + 1). Value = swBOMAnnotation.Text(I, J)
Next J
Next I

boolstatus = swModelDocExt.SelectByID2(swBOMFeature.GetFeature.Description, "BOMFEATURE", 0, 0, 0, True, 0, Nothing, 0)
swModel.EditDelete

swModel.ForceRebuild3 True

Dim config As SldWorks.Configuration
Dim cusPropMgr As SldWorks.CustomPropertyManager
Dim lRetVal As Long
Dim ValOut As String
Dim ResolvedValOut As String
Dim wasResolved As Boolean
Dim nNbrProps As Long
Dim vPropNames As Variant
Dim vPropTypes As Variant
Dim vPropValues As Variant
Dim resolved As Variant
Dim custPropType As Long
Dim K As Long
Dim NameProperty As String

Set cusPropMgr = swModelDocExt.CustomPropertyManager("")

nNbrProps = cusPropMgr.Count
vPropNames = cusPropMgr.GetNames
For K = 0 To nNbrProps - 1
cusPropMgr.Get2 vPropNames(K), ValOut, ResolvedValOut
custPropType = cusPropMgr.GetType2(vPropNames(K))
If vPropNames(K) = "CARTOONIST" Then
PropertyName = ResolvedValOut
End If
Next K

Dim Path As String
path = About("USERPROFILE") & "\Desktop\" & swModel.GetTitle & "-" & PropertyName & ".xls"

With xlApp
wbk. SaveAs path
wbk. Close
. Quit
End With

End Sub

Hello

I think you should either initialize your Configuration variable before inserting your BOM with for example Configuration = "" or replace Configuration with "" in the line Set swBOMAnnotation = swModelDocExt.InsertBomTable3(TemplateName, 0, 0, BomType, Configuration, False, swNumberingType_Detailed, True).

Kind regards

1 Like

thank you Roger for your answer.

I realize that I hadn't copied the line that said:

Configuration = "Default"

For more flexibility, I just changed this line to 

Configuration = swApp.GetActiveConfigurationName(sFileName)

by having well beforehand

Const sFileName                 As String = "test. SLDPRT"

My problem always occurs with:

Set swBOMFeature = swBOMAnnotation.BomFeature

where swBOMFeature = Nothing

FYI on the previous line, I also swBOMAnnotation=Nothing

Is this normal?

thank you for your help

 

Hello

First point: You must first check that the TemplateName = "C:\Users\sal1chl\Desktop\test_nomenclature.sldbomtbt" is written correctly because if it is not the case it will block on the line Set swBOMFeature = swBOMAnnotation.BomFeature.

Second point: If you use the macro on a part ("test. SLDPRT") then the BomType can't be  "swBomType_TopLevelOnly" and then it gets stuck on the line Set swBOMFeature = swBOMAnnotation.BomFeature, change it to "swBomType_Indented" to check if it's coming from there. Strange to want to put a nomenclature on a part!

Kind regards

1 Like

Hello Roger,

A big thank you, it works!

Do you know how to automatically retrieve the name of the assembly in which I am running this macro?

For the moment, I give it the name in the macro

Const sFileName                 As String = "test_assemblage. SLDASM"

Thank you

 

Kind regards

 

LSE

Something like this should give you the name of the file (to be added under your line: Set swModel = swApp.ActiveDoc)

 

Dim sFileName as string

sFileName =swModel.GetPathName

Debug.print sFileName

PS: don't forget to delete this line: Const sFileName                 As String="test_assemblage. SLDASM"  (or put it in a commentary)

1 Like

Hello Denis,

My answer had not gone away... sorry (I didn't have to select "publish"). Thank you it worked great.

And then, I don't know what I did because nothing works anymore...

NumRow = swBOMAnnotation.RowCount  gives 1 while I have several components... My nomenclature is no longer displayed at all...

Do you have an idea?

I attach the macro to you...

thank you for your help


insert-bom-asm.swp

One more little clarification....

when I manually insert my BOM by calling 

TemplateName = "C:\Users\sal1chl\Desktop\test_assemblage2.sldbomtbt"
 

No worries, the table is displayed (1st level)

if I call it via the macro, I only have the 1st row of the table with the names of the columns....

I guess my mistake is in there...

TemplateName = "C:\Users\sal1chl\Desktop\test_assemblage.sldbomtbt"
' BOM Type

BomType = swBomType_TopLevelOnly
'Creation of the BOM table

Set swBOMAnnotation = swModelDocExt.InsertBomTable3(TemplateName, 0, 0, BomType, Configuration, False, swNumberingType_Detailed, True)
Set swBOMFeature = swBOMAnnotation.BomFeature

swModel.ForceRebuild3 True

Dim NumCol As Long
Dim NumRow As Long
Dim I As Long
Sun J As Long

NumCol = swBOMAnnotation.ColumnCount              ' it gives 5
NumRow = swBOMAnnotation.RowCount                  ' it gives 0 hence the problem....

Hello

I found my mistake...

I had inadvertently swapped the lines SFileName and Configuration.

 

sFileName = swModel.GetPathName '= display name of the assembly with the path
Debug.Print sFileName

Configuration = swApp.GetActiveConfigurationName(sFileName)

 

Issue Resolved

thank you to both of you for your help

 

LSE