Macro to delete General Table by Name not working

As part of a larger Macro, I need to scan drawing files for general tables with specific names and delete them.  I've tried several methods from examples found here or from CodeStack, but can't get them to work.  Either nothing happens or I get the message "None of the selected entities could be deleted".  Here's the code that shows several methods I have tried.  Any advice is appreciated.

I'm not sure of the best way to paste code, so here's my attempt

Sub main()

    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim BoolStatus                  As Boolean

    Dim swFeat                      As SldWorks.Feature

    Dim swSubFeat                   As SldWorks.Feature

    Dim TARGET_TABLE_NAME           As String

 

    TARGET_TABLE_NAME = "General Table3" ' This is the name of the General Table to delete

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swFeat = swModel.FirstFeature

 

    ' Step through the feature tree, looking for a GENERAL TABLE with a name that matches TARGET_TABLE_NAME

    Do Until swFeat Is Nothing

        Set swSubFeat = swFeat.GetFirstSubFeature

         Do Until swSubFeat Is Nothing

            If swSubFeat.GetTypeName = "GeneralTableFeature" Then

                If swSubFeat.Name = TARGET_TABLE_NAME Then

                    Debug.Print "Bingo"

                    BoolStatus = swModel.Extension.SelectByID2(swSubFeat.Name, swSubFeat.GetTypeName, 0, 0, 0, False, 0, Nothing, 0)

                    'Delete attempt #1

                        Call swModel.Extension.SelectByID2(swSubFeat.Name, swSubFeat.GetTypeName, 0, 0, 0, False, 0, Nothing, 0)

                    'Delete attempt #2

                        Call swModel.Extension.SelectByID2(swSubFeat.Name, swSubFeat.GetTypeName, 0, 0, 0, True, 0, Nothing, 0)

                    'Delete attempt #3

                        swModel.EditDelete

                        BoolStatus = swModel.DeleteSelection(False)

                    'Delete attempt #4

                        swModel.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Absorbed

                        BoolStatus = swModel.DeleteSelection(False)

                End If

            End If

            Set swSubFeat = swSubFeat.GetNextSubFeature

        Loop

        Set swFeat = swFeat.GetNextFeature

    Loop

End Sub

SolidworksDrawings And Detailing
Answers 7
RE
RE 2021-04-08 Best Answer

Hi Mark,

try that:

vSheets = swDraw.GetViews
For i = 0 To UBound(vSheets)
   vViews = vSheets(i)
   For j = 0 To UBound(vViews)
      vTableAnnots = vViews(j).GetTableAnnotations
      If Not IsEmpty(vTableAnnots) Then
         For k = 0 To UBound(vTableAnnots)
            If vTableAnnots(k).Type = 0 Then
               Set swTable = vTableAnnots(k)
               Set swGenTabFeat = swTable.GeneralTableFeature
               Set swFeat = swGenTabFeat.GetFeature
               swFeat.Select2 False, -1
               bRet = swModel.Extension.DeleteSelection2(2)
            End If
         Next
      End If
   Next
Next

Kind regards

Ronny

DG
DG 2021-04-08

Mark Soldan wrote:

Can you point me to a reference where I can ready more about this phenomenon?

SW API Help should help you with that 2019 SOLIDWORKS API Help - swSelectType_e Enumeration 

MS
MS 2021-04-08

Thank you Deepak.  I was able to get my original code to work with your help.

That's crazy. I was looking for a feature of the type "GeneralTableFeature" instead of type "GENERALTABLEFEAT"  because "GeneralTableFeature" is what is reported in the immediate window.  Can you point me to a reference where I can ready more about this phenomenon?

MS
MS 2021-04-08

Ronny, I figured out how to get your code to only look on the current sheet.  Thanks for your help.

DG
DG 2021-04-08

Replace

BoolStatus = swModel.Extension.SelectByID2(swSubFeat.Name, swSubFeat.GetTypeName, 0, 0, 0, False, 0, Nothing, 0)

with

BoolStatus = swModel.Extension.SelectByID2(swSubFeat.Name, "GENERALTABLEFEAT", 0, 0, 0, False, 0, Nothing, 0)

MS
MS 2021-04-08

Thank you, Ronny.  It took me a while to figure out the variable declarations, but I was able to get your code to work.  Now to figure out how to make it delete tables by name.