VBA - User Interaction and BOM

Hi all 

Let me explain my situation to you. I work on assemblies of several hundred parts and I have to export some part plans in PDF. To do this, I insert a BOM table into my assembly. The last column of the table tells me if we want the plan of the room or not. Then I export the plans that interest me in PDF one by one.

I would like to make a program that automates all this a little to save me time. I would like the user to enter the part number in a dialog box (e.g. "25" to select the 25th part of the table). Then, the program would open the plan associated with the selected room and save it as a PDF. 

The second part of the program is not a problem for me, but the part selection of the part by its nomenclature number is a problem for me. I can't make a program capable of reading and finding the 25th part of the nomenclature table and opening the plan of this part. If you have any ideas, it would help me a lot.

Hoping to be clear enough:)

Thank you for your help

Hello;

Here's an example for counting the lines:
http://help.solidworks.com/2020/English/api/sldworksapi/Get_Components_in_Each_BOM_Table_Row_VB.htm?verRedirect=1

The variable you should be interested in is: nNumRow = swTableAnn. RowCount

Another Macro to open a plan directly from a Nomenclature:

https://r1132100503382-eu1-3dswym.3dexperience.3ds.com/#community:yUw32GbYTEqKdgY7-jbZPg/post:YxLaeuf8RRWPjMc6PF9ulA

' ###################################################
' # Title: Open Drawing From BOM                    #
' # Version: 21.9.6                                 #
' # Author: Stefan Sterk                            #
' # Company: Idee Techniek Engineering B.V.         #
' #                                                 #
' # This macro will try to open the drawing for the #
' # selected component(s) in the Bill of Meterials. #
' #                                                 #
' # NOTE: Drawing file must be in same folder as    #
' # component and must have the same filename       #
' ###################################################
 
Option Explicit
 
Dim swApp As SldWorks.SldWorks
 
Sub main()
 
    Dim swModel  As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swTblAnn As SldWorks.TableAnnotation
    Dim swBOMTbl As SldWorks.BomTableAnnotation
    Dim swComp   As SldWorks.Component2
 
    Dim i As Integer, selType  As Integer
    Dim frtRow As Long, lstRow As Long
    Dim frtCol As Long, lstCol As Long
    Dim Row As Integer
 
    Dim vComps   As Variant
    Dim CfgName  As String
 
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
 
    If swModel Is Nothing Then Exit Sub
    If Not swModel.GetType = swDocDRAWING Then Exit Sub
 
    Set swSelMgr = swModel.SelectionManager
 
    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
 
        selType = swSelMgr.GetSelectedObjectType3(i, -1)
 
        If selType <> 98 Then
            MsgBox "Please select a cel from BOM!"
            Exit Sub
        End If
 
        Set swTblAnn = swSelMgr.GetSelectedObject6(i, -1)
        Set swBOMTbl = swTblAnn
 
        swTblAnn.GetCellRange frtRow, lstRow, frtCol, lstCol
 
        For Row = frtRow To lstRow
            CfgName = swBOMTbl.BomFeature.GetConfigurations(True, True)(0)
            vComps = swBOMTbl.GetComponents2(Row, CfgName)
            If Not IsEmpty(vComps) Then
                Set swComp = swBOMTbl.GetComponents2(Row, CfgName)(0)
                openComponentDrawing swComp
            End If
        Next Row
 
    Next i
End Sub
 
Private Function openComponentDrawing(swComp As Component2)
 
    Dim compPath As String
    compPath = swComp.GetPathName
 
    Dim drwPath As String
    drwPath = Left(compPath, InStrRev(compPath, ".") - 1) & ".slddrw"
 
    ' Try Open Drawing
    Dim swDrw As SldWorks.DrawingDoc
    Dim errors As Long, warnings As Long
    Set swDrw = swApp.OpenDoc6(drwPath, swDocDRAWING, 0, "", errors, warnings)
 
    If errors <> 0 Then
        If errors = 2 Then
            Dim partNumber As String
            partNumber = Right(drwPath, Len(drwPath) - InStrRev(drwPath, "\"))
            partNumber = Left(partNumber, InStrRev(partNumber, ".") - 1)
            MsgBox "Couldn't find drawing for following part number: " & partNumber
        End If
    Else
        swApp.ActivateDoc3 drwPath, False, 0, errors
    End If
 
End Function

Kind regards.

2 Likes