MACRO LIBRARY

Change table cell text color

Free public access. No account or subscription required.

Files

Change table cell text color

Code

Module: mMain

'Demonstrates how to change a cell's text color.
    
'Preconditions: Table is selected.
'Results: Specified cell's text color is changed to the specified code.
    
'Notes:
'The hex code is not the same as the standard HTML color codes. To find _
    the correct color code: _
    1. In the GUI, chance a cell color _
    2. Right-click on the 4-arrow box in the top left of table-->Save As .txt _
    3. Open the text file and see what code is being used
'If you try to change a weldment table value that is derived from _
    the model, it might not change. This is because such cells in weldment _
    tables can't be edited, manually or programmatically.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Const ROW As Integer = 3
Const COL As Integer = 2
Const COLOR_CODE As String = "0x000000ff"
    
Option Compare Text
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swTableAnn As SldWorks.TableAnnotation
    Dim strCellValue As String
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    
    If swSelMgr.GetSelectedObjectCount2(-1) = 0 Then Exit Sub
    If swSelMgr.GetSelectedObjectType3(1, -1) <> swSelANNOTATIONTABLES Then
        swApp.SendMsgToUser "Please select a table."
        Exit Sub
    End If
    
    Set swTableAnn = swSelMgr.GetSelectedObject6(1, -1)
    
    strCellValue = swTableAnn.Text(ROW, COL)
    
    Dim vSplit As Variant
    If InStr(strCellValue, "</font>") <> 0 Then
        vSplit = Split(strCellValue, ">")
        vSplit = Split(vSplit(0), "=")
        strCellValue = Replace(strCellValue, vSplit(1), COLOR_CODE)
        swTableAnn.Text(ROW, COL) = strCellValue
    Else
        swTableAnn.Text(ROW, COL) = "<font color=" & COLOR_CODE & ">" & strCellValue & "</font>"
    End If
    
    Debug.Print swTableAnn.Text(ROW, COL)
End Sub