0

I need to get the selected cells of a table.

I tried the following code

Set oTbl = .ShapeRange(1).Table
For x = 1 To oTbl.Rows.Count
    For y = 1 To oTbl.Columns.Count
        If oTbl.Cell(x, y).Selected Then
            With oTbl.Cell(x, y).Shape
                .Fill.ForeColor.RGB = RGB(255, 0, 0)
                .Fill.Visible = True
            End With
        End If
    Next
Next

This works fine if you are using vba, but im creating a vsto using vb.net and it colors the complete table using the below code

Dim oShape As PowerPoint.Shape = Nothing
    Dim oTable As PowerPoint.Table

    With Globals.ThisAddIn.Application.ActiveWindow.Selection

        For Each oShape In .ShapeRange
            oShape.Select()
            Exit For
        Next

        oTable = oShape.Table

        Dim i As Integer = 0
        Dim j As Integer = 0

        For i = 1 To oTable.Rows.Count
            For j = 1 To oTable.Columns.Count
                If oTable.Cell(i, j).Selected = True Then
                    With oTable.Cell(i, j).Shape
                        .Fill.ForeColor.RGB = RGB(255, 0, 0)
                        .Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
                    End With
                End If
            Next
        Next

    End With

Please help

Thanks

Leroy M

Intricate
  • 117
  • 1
  • 11
  • You might want to describe the problem you're having. You don't give us any hint of what's going wrong. But for starters, it looks like your logic is wrong. Your For/Next loop selects the first shape on the slide and exits. It may or may not leave you with an active reference to the selected shape and if the current selected shape isn't a table, your code will fail at oTable=oShape.Table – Steve Rindsberg Jul 18 '13 at 15:14
  • Hello Steve, i will be selecting a table, can you please take the code from Dim i and j as integer. note that this code is simply to run a demo from the button. Will handle later if its a table or not. The logic is fine and works well, just use the initial code in your vba for powerpoint and then the second code in vb.net vsto for powerpoint, you will find the difference – Intricate Jul 21 '13 at 06:45
  • Your first block of code `For Each oShape In .ShapeRange ...` changes the selection. You are selecting the whole shape. It doesn't matter what was selected before, because you change the selection in code, so of course it selects the whole table. – BrainSlugs83 May 23 '16 at 22:22

2 Answers2

0

This works in VBA; I don't use .Net so can't help with that.

Sub TableTest()

    Dim oSh As Shape
    Dim oTbl As Table
    Dim lRow As Long ' your i
    Dim lCol As Long ' your j
    Set oSh = ActiveWindow.Selection.ShapeRange(1)
    Set oTbl = oSh.Table

    With oTbl
        For lRow = 1 To .Rows.Count
            For lCol = 1 To .Columns.Count
                If .Cell(lRow, lCol).Selected Then
                    With .Cell(lRow, lCol).Shape
                        .Fill.ForeColor.RGB = RGB(255, 0, 0)
                        .Fill.Visible = True
                    End With
                End If
            Next
        Next
    End With

End Sub
Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34
0

Your first block of code in VB.NET, changes the selection:

For Each oShape In .ShapeRange
    oShape.Select()
    Exit For
Next

The shape which contains the whole table, and is a part of the selection, is being re-selected by this block -- This causes the whole shape and table to be selected at the start of your code.

Remove this block of code, and the rest should work.

BrainSlugs83
  • 6,214
  • 7
  • 50
  • 56