MACRO LIBRARY

Copy table to clipboard

Free public access. No account or subscription required.

Files

Copy table to clipboard

Code

'Copies the selected table's contents to the clipboard.
    
'Preconditions: table is selected
    
'Note that this macro references the Microsoft Forms 2.0 Object Library
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then End
    
    'Get selected table
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swTableAnn As SldWorks.TableAnnotation
    Set swSelMgr = swModel.SelectionManager
    Set swTableAnn = swSelMgr.GetSelectedObject6(1, -1)
    If swTableAnn Is Nothing Then
        swApp.SendMsgToUser "Please select a table."
        End
    End If
    
    'Traverse table contents
    Dim i As Integer
    Dim j As Integer
    Dim strPasteText As String
    
    strPasteText = Empty
    For i = 0 To swTableAnn.RowCount - 1
        For j = 0 To swTableAnn.ColumnCount - 1
            strPasteText = strPasteText & swTableAnn.DisplayedText(i, j) & vbTab
        Next j
        strPasteText = strPasteText & vbCrLf
    Next i
    
    'Copy contents to clipboard
    Dim myDO As New MSForms.DataObject
    myDO.SetText strPasteText, 1
    myDO.PutInClipboard
End Sub