vba code error with macro recorder

Hello everyone,

I made a code with the macro recorder in solidworks 2018 and when I launch it I get an error "Object variable or block variable with not defined"

Here's the code

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Dim swActiveView As Object
Set swActiveView = Part.ActiveDrawingView
Dim swBOMTable As Object
Set swBOMTable = swActiveView.InsertBomTable2(False, 1.96979560481769E-02, 0.287303519163763, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft, swBomType_e.swBomType_TopLevelOnly, "", "C:\Program Files\SolidWorks Corp\SolidWorks\lang\french\bom-standard.sldbomtbt")
boolstatus = Part.EditRebuild3()
boolstatus = Part.Extension.SelectByID2("Objet de détail2@Feuille1", "ANNOTATIONTABLES", 4.85493846196055E-02, 0.280618432055749, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Objet de détail2@Feuille1", "ANNOTATIONTABLES", 5.24196982084557E-02, 0.252470696864111, 0, False, 0, Nothing, 0)
End Sub

 

The error is located at the line:

Set swBOMTable = swActiveView.InsertBomTable2(False, 1.96979560481769E-02, 0.287303519163763, swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft, swBomType_e.swBomType_TopLevelOnly, "", "C:\Program Files\SolidWorks Corp\SolidWorks\lang\french\bom-standard.sldbomtbt")

And I don't understand why because I created the macro with the same file that I'm using, so it should work.

Thank you in advance for your answers

The error comes from the fact that the view is not selected.

If you manually select a view before launching the macro it should work.

Otherwise add a code to do it automatically. Like what:

Option Explicit
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDraw As SldWorks.DrawingDoc
    Dim swActiveView As SldWorks.View
    Dim swBOMTable As SldWorks.BomTableAnnotation
    Dim Config As String
    Dim TemplateName As String
    Dim BomType As Long
    Dim AnchorType As Long

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swActiveView = swDraw.GetFirstView
    Set swActiveView = swActiveView.GetNextView
    swModel.Extension.SelectByID2 swActiveView.Name, "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0

    Config = swActiveView.ReferencedConfiguration
    TemplateName = "C:\Program Files\SolidWorks Corp\SolidWorks\lang\french\bom-standard.sldbomtbt"
    BomType = swBomType_e.swBomType_TopLevelOnly
    AnchorType = swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft

    Set swBOMTable = swActiveView.InsertBomTable2(False, 0, 0.287, AnchorType, BomType, Config, TemplateName)
End Sub

 

1 Like

Many thanks JeromeP,

I'm not used to the vba in solidworks and so I'm quickly lost.

Can you tell me how (or give me a lead) to export this nomenclature in csv or xls.

Because the macro recorder doesn't save the file naming and the file type, so I don't have any basic command.

It must go through a " SaveAs Filename:=" and FileFormat:=xlCSV, because it 's still vba but for me I use it in excel so there must be variations.

1 Like

There are several ways to do this. Like what:

Add this line to the end of the previous code:

WriteCsv "C:\Temp\myFile.csv", swBOMTable

As well as the function:

Sub WriteCsv(filePath As String, table As SldWorks.TableAnnotation)
    Dim fileNmb As Integer
    fileNmb = FreeFile
    Open filePath For Output As #fileNmb
    Dim i As Integer
    Dim j As Integer
    Dim row As String
    Dim cell As String
    For i = 0 To table.RowCount - 1
        row = ""
        For j = 0 To table.ColumnCount - 1
            cell = table.Text(i, j)
            If InStr(cell, ",") > 0 Or InStr(cell, vbLf) > 0 Or InStr(cell, vbNewLine) > 0 Then cell = """" & cell & """"
            row = row & IIf(j = 0, "", ",") & cell
        Next
        Print #fileNmb, row
    Next
    Close #fileNmb
End Sub

Adapted from https://www.codestack.net/solidworks-api/document/tables/export-table-csv/

Ok thank you for your answer JeromeP

I'm going to look at it closely

On the other hand, where did you learn to code vba for solidworks and how did you learn?

Are you a developer or a solidworks user?

Basically I'm a SW user and I learned by modifying the code produced by the macro recorder :)

Now I do more development. By the way, if you need to develop a more complicated code , let me know.

1 Like