Export of Solidworks BOM by VB

Hi all!

I have a small macro on the Solidworks API to export the BOM of an open drawing to Excel in . CSV

What I would like is for this Excel file to have a fixed name and every time I run my macro, the exported BOM table from Solidworks comes to write just below the other one. I don't know enough about the code I was given, what it currently does is create a single array. I can't incorporate the instruction "write only from the moment it's empty", just to have my nomenclatures that follow one another as I execute the macro on my different planes.

If anyone has a line of code that would help me, I'm all for it!!

Have a nice day.

 


20158271458773_export_bom.swp
1 Like

Hello

For example, if in a CSV file, we use the semicolon as a separator and we want to write the letters A, B and the variable C on the first line and the letters D, F and the variable F on the first line , we should write:

f = FreeFile
Open "C:\TEST.csv" for append as #f
Print #f, "A; B; " & C
Print #f, "E; F; " & G

Close #f
 

With a concrete example (from the implementation of attachments in our ERP):

f = FreeFile

Open "\\dimecox3\PLANBE$\Import_plans.csv" For Append As #f opening CSV or TXT file
    Writing to the properties file
                Print #f, "ITM; 2; " & CODE & ";; 001; " & docNamePDF & "; " & "PDF"
                Print #f, "ITM; 2; " & CODE & ";; 002; " & docNameDWG & "; " & "DWG"
Close #f

2 Likes

Ouch! I don't feel like it's the solution to my problem, I'm a little lost... Have you opened the code as an attachment to see the result (you need a drawing with a bill of materials   for it to work).

Macro works very well! but I just want that at each RUN of the macro, the nomenclature active in solidworks starts on the same csv file, but below what may have been written appears. In this way, by going through my plans and executing the macro, I would end up with an excel that globalizes all my parts.

Clearly, I'm bad, I'm just starting out! I'm a cartoonist, and I'm discovering the power of these APIs:)

 

1 Like

My answer answers this problem well, but I didn't open the SWP file because I don't have SolidWorks at hand.

Can you post the macro in a text file or directly in a reply please?

1 Like

Sub ProcessBomFeature(swBomFeat As SldWorks.BomFeature)

    Dim vTableArr as Variant
    Dim vTable as Variant
    Dim swTable As SldWorks.TableAnnotation
    Dim swAnn As SldWorks.Annotation
    Dim nNumCol As Long
    Dim nNumRow As Long
    Dim sRowStr As String
    Sun i As Long
    Sun j As Long
    
    'Retrieving Annotation Tables
    vTableArr = swBomFeat.GetTableAnnotations
    
    'Traversing Annotation Tables
    For Each vTable In vTableArr
    
        'Retrieving a table
        Set swTable = vTable

 'Content recovery
        Set swAnn = swTable.GetAnnotation
    
        'Number of columns and rows
        nNumCol = swTable.ColumnCount
        nNumRow = swTable.RowCount
    
        'Table Name and Type
        Debug.Print "    Table : " & swAnn.GetName & " Type : " & swTable.Type
    
        'Route of the lines
        For i = 0 To nNumRow - 1
    
            sRowStr = "      "
    
            'Column route
            For j = 0 To nNumCol - 1
            
                sRowStr = sRowStr & swTable.Text(i, j) & " | "
                
            Next j
            
        ' Displaying Content
        Debug.Print Left(sRowStr, Len(sRowStr) - 1)
    
        Next i
        


        'Exit folder
        Dim xlsPath As String
        xlsPath = Mid(swModel.GetPathName, 1, InStrRev(swModel.GetPathName, "\"))
        
        'Name of the XLS
        Dim xlsName As String
        If InStrRev(swModel.GetTitle, ".") > 0 Then
            xlsName = Mid(swModel.GetTitle, 1, InStrRev(swModel.GetTitle, ".") - 1) & ".csv"
        Else
            xlsName = swModel.GetTitle & ".csv"
        End If
        
        Debug.Print "Output: " & xlsPath & xlsName
        
        Export Excel
        swTable.SaveAsText xlsPath & xlsName, "; "
        
    Next vTable
    
    MsgBox "Export Completed"

End Sub

 

So this piece works nikel if I only had a nomenclature to export. The; as separators do their job and everything.

So that's what I thought: the SaveAsText function used doesn't allow you to add lines, if the file already exists it's overwritten: 


If a file of the specified name in the specified path...   Exists    

Then it is...  Overwritten

Source: http://goo.gl/odfZb9

It is necessary to go through the solution proposed in my first message.

 
1 Like

A big thank you to you .PL!!

1 Like

With pleasure. If you can post your macro with the code modified for people who will have the same problem, and in a text file, even better!

1 Like