Cell Background Fill

Discussion in 'AutoCAD' started by Ben Rand, May 24, 2004.

  1. Ben Rand

    Ben Rand Guest

    I'm trying to fill a cell with a background color via VBA. My code (below)
    doesn't work, although the same code altered to SetCellContentColor works
    fine, and retrieving the cell background color (via GetCellBackgroundColor)
    with essentially this code also works. Is the SetCellBackgroundColor method
    just broken?

    Public Sub ShadeCell()
    Dim oEnt As AcadEntity
    Dim oTable As AcadTable
    Dim R As Long
    Dim C As Long
    Dim Util As AcadUtility
    Dim vPt As Variant
    Dim vVec(0 To 2) As Double
    Dim color As AcadAcCmColor

    Set Util = ThisDrawing.Utility

    On Error Resume Next
    Util.GetEntity oEnt, vPt, vbCrLf & "Select cell to shade: "
    If oEnt Is Nothing Or Not TypeOf oEnt Is AcadTable Then
    MsgBox "You must select a table object."
    GoSub Cleanup
    Else
    Set color = New AcadAcCmColor
    color.ColorMethod = acColorMethodByACI
    color.ColorIndex = 254
    Set oTable = oEnt
    vVec(0) = 0: vVec(1) = 0: vVec(2) = 1
    oTable.HitTest vPt, vVec, R, C
    oTable.SetCellBackgroundColor R, C, color
    End If

    Cleanup:
    Set Util = Nothing
    Set oTable = Nothing
    Set oEnt = Nothing
    Set color = Nothing
    End Sub

    Ben Rand
    CAD Manager
    CEntry Constructors & Engineers
     
    Ben Rand, May 24, 2004
    #1
  2. Ben,

    You need to tell AutoCAD that there _is_ a background to show:

    oTable.HitTest vPt, vVec, R, C
    oTable.SetCellBackgroundColorNone R, C, False '<- added
    oTable.SetCellBackgroundColor R, C, color


    --
    R. Robert Bell


    I'm trying to fill a cell with a background color via VBA. My code (below)
    doesn't work, although the same code altered to SetCellContentColor works
    fine, and retrieving the cell background color (via GetCellBackgroundColor)
    with essentially this code also works. Is the SetCellBackgroundColor method
    just broken?

    Public Sub ShadeCell()
    Dim oEnt As AcadEntity
    Dim oTable As AcadTable
    Dim R As Long
    Dim C As Long
    Dim Util As AcadUtility
    Dim vPt As Variant
    Dim vVec(0 To 2) As Double
    Dim color As AcadAcCmColor

    Set Util = ThisDrawing.Utility

    On Error Resume Next
    Util.GetEntity oEnt, vPt, vbCrLf & "Select cell to shade: "
    If oEnt Is Nothing Or Not TypeOf oEnt Is AcadTable Then
    MsgBox "You must select a table object."
    GoSub Cleanup
    Else
    Set color = New AcadAcCmColor
    color.ColorMethod = acColorMethodByACI
    color.ColorIndex = 254
    Set oTable = oEnt
    vVec(0) = 0: vVec(1) = 0: vVec(2) = 1
    oTable.HitTest vPt, vVec, R, C
    oTable.SetCellBackgroundColor R, C, color
    End If

    Cleanup:
    Set Util = Nothing
    Set oTable = Nothing
    Set oEnt = Nothing
    Set color = Nothing
    End Sub

    Ben Rand
    CAD Manager
    CEntry Constructors & Engineers
     
    R. Robert Bell, May 24, 2004
    #2
  3. Ben Rand

    Ben Rand Guest

    Robert,

    How in the world did you figure that one out? Many thanks, your answer
    worked great.

    Ben
     
    Ben Rand, May 24, 2004
    #3
  4. Dogged determination!

    --
    R. Robert Bell


    Robert,

    How in the world did you figure that one out? Many thanks, your answer
    worked great.

    Ben
     
    R. Robert Bell, May 24, 2004
    #4
  5. Ben Rand

    Ben Rand Guest

    My kind of programmer!

     
    Ben Rand, May 24, 2004
    #5
  6. What is *really* cool, is that using this thread as a basis, I came up with
    a function to apply the "background mask" to a cell! See the 2005 ng for the
    function, if you wish.
     
    R. Robert Bell, May 24, 2004
    #6
  7. Ben Rand

    Ben Rand Guest

    Nice!
     
    Ben Rand, May 25, 2004
    #7
  8. Ben Rand

    Ben Rand Guest

    Variation on that theme:

    Sub SetTableToBackground()
    Dim oEnt As AcadEntity
    Dim oTable As AcadTable
    Dim R As Long
    Dim C As Long
    Dim Util As AcadUtility
    Dim vPt As Variant
    Dim newColor As AcadAcCmColor

    Set Util = ThisDrawing.Utility

    On Error Resume Next
    Util.GetEntity oEnt, vPt, vbCrLf & "Select cell to add background mask: "
    If TypeOf oEnt Is AcadTable Then
    Set oTable = oEnt
    oTable.SetBackgroundColorNone acDataRow + acHeaderRow + acTitleRow,
    False
    Set newColor = oTable.GetBackgroundColor(acUnknownRow)
    newColor.SetRGB 0, 0, 0
    oTable.SetBackgroundColor acDataRow + acHeaderRow + acTitleRow, newColor
    End If

    Set oEnt = Nothing
    Set Util = Nothing
    Set oTable = Nothing
    Set newColor = Nothing
    End Sub

    Ben
     
    Ben Rand, May 25, 2004
    #8
  9. Also nice! although I would suggest revising the prompt just a smidge.

    --
    R. Robert Bell


    Variation on that theme:

    Sub SetTableToBackground()
    Dim oEnt As AcadEntity
    Dim oTable As AcadTable
    Dim R As Long
    Dim C As Long
    Dim Util As AcadUtility
    Dim vPt As Variant
    Dim newColor As AcadAcCmColor

    Set Util = ThisDrawing.Utility

    On Error Resume Next
    Util.GetEntity oEnt, vPt, vbCrLf & "Select cell to add background mask: "
    If TypeOf oEnt Is AcadTable Then
    Set oTable = oEnt
    oTable.SetBackgroundColorNone acDataRow + acHeaderRow + acTitleRow,
    False
    Set newColor = oTable.GetBackgroundColor(acUnknownRow)
    newColor.SetRGB 0, 0, 0
    oTable.SetBackgroundColor acDataRow + acHeaderRow + acTitleRow, newColor
    End If

    Set oEnt = Nothing
    Set Util = Nothing
    Set oTable = Nothing
    Set newColor = Nothing
    End Sub

    Ben
     
    R. Robert Bell, May 25, 2004
    #9
  10. Ben Rand

    Ben Rand Guest

    Caught that after I posted--oops.

     
    Ben Rand, May 25, 2004
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.