MACRO LIBRARY

Save selected table as Excel spreadsheet

Free public access. No account or subscription required.

Files

Save selected table as Excel spreadsheet

Code

Module: mMain

'SaveSelectedTable() will save out the selected table as an XLS.
'SaveFirstTable() will save out the first table of the type specified _
    in TABLE_TYPE to an XLS.
    
'Preconditions
'--SaveSelectedTable: drawing is open and weldment table selected.
'--SaveFirstTable: drawing is open.
'--Desired save directory is specified in SAVE_FOLDER
    
'Notes
'--When prompted, enter the desired XLS name without the extension.
'--The XLS file saved out is not a true Excel file, which is why when you _
    open it you will be warned that the file has the wrong extension. You _
    must resave the spreadsheet to prevent this dialog in the future.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Const SAVE_FOLDER As String = "C:\"
Const TABLE_TYPE As Long = swTableAnnotationType_e.swTableAnnotation_WeldmentCutList
    
Sub SaveSelectedTable()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swTable As SldWorks.TableAnnotation
    Dim i As Integer
    Dim strXlsTitle As String
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swTable = swSelMgr.GetSelectedObject6(1, -1)
    
    strXlsTitle = InputBox("Enter the document name. Extension will " & _
        "be added automatically. Current save directory is: " & _
        vbCrLf & SAVE_FOLDER)
    
    If strXlsTitle = Empty Then Exit Sub
    
    If swTable.SaveAsText(SAVE_FOLDER & strXlsTitle & ".xls", "") = _
        False Then swApp.SendMsgToUser "Failed to save table."
End Sub
    
Sub SaveFirstTable()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swDraw As SldWorks.DrawingDoc
    Dim swView As SldWorks.View
    Dim swTable As SldWorks.TableAnnotation
    Dim vTables As Variant
    Dim i As Integer
    Dim strXlsTitle As String
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swView = swDraw.GetFirstView
    vTables = swView.GetTableAnnotations
    
    For i = 0 To UBound(vTables)
        Set swTable = vTables(i)
        If swTable.Type = TABLE_TYPE Then
            
            strXlsTitle = InputBox("Enter the document name. Extension will " & _
                "be added automatically. Current save directory is: " & _
                vbCrLf & SAVE_FOLDER)
            
            If strXlsTitle = Empty Then Exit Sub
            
            If swTable.SaveAsText(SAVE_FOLDER & strXlsTitle & ".xls", "") = _
                False Then swApp.SendMsgToUser "Failed to save table."
                
            Exit Sub
        End If
    
    Next i
    
    swApp.SendMsgToUser "No weldment table found."
End Sub