2 Replies Latest reply on Jan 8, 2015 8:15 AM by Chris Angevine

    Change Table Text Color Through API

    Chris Angevine

      I'm looking for a way in API to change the color of text in certain cells of a Weldment Cutlist Table.  After searching past posts I found one that can do it, but it also changes the text itself.

      swTable.Text(0, 0) = "<FONT color=0x000000ff>Red Color<FONT color=0x00000000>"


      I want to leave the text as it is and just change the color.  Changing the background color would be fine too.  Has anyone done this before?



        • Re: Change Table Text Color Through API
          Keith Rice



          Edit: Explained how to find color code at the bottom of the post.


          The following code will change a cell's text color using the API. Note that this code might not work on weldment properties derived from the model, at least on the weldment model I tried. All edits, whether done manually or programmatically, seem to be blocked. Perhaps someone more knowledgeable in weldments can explain why this is. Nevertheless, this code should work on any table (so long as that cell can be edited).


          To use this code, specify the row, column, and color code:


          'Demonstrates how to change a cell's text color using the API.
          'Preconditions: Table is selected.
          'Results: specified cell's text color is changed to the specified code.
          'Written by Keith Rice
          'CADSharp LLC
          Const ROW As Integer = 0
          Const COL As Integer = 0
          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
                  swTableAnn.Text(ROW, COL) = "<font color=" & COLOR_CODE & ">" & strCellValue & "</font>"
              End If
              Debug.Print swTableAnn.Text(ROW, COL)
          End Sub


          Much of the code here serves no other purpose than allowing you to change color on the same cell multiple times. That is why I am searching to see if the HTML tag already exists.


          Finding the color code:


          Even though an HTML tag is being used to specify the text color, those aren't standard HTML color codes. I'm not sure what the scheme is. Regardless, you can figure out what code you need to use by doing this:


          1. In the GUI, change a cell color.

          2. Right-click on the 4-arrow box in the top left of the table-->Save As .txt.

          3. Open the txt file and see what code is being used.



          SolidWorks API Tutorials