在表powerpoint 2007 vb.net中获取选定的单元格

时间:2013-07-18 07:04:07

标签: vb.net macros powerpoint visual-studio-macros

我需要获取表格的选定单元格。

我尝试了以下代码

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

如果您使用vba,这可以正常工作,但我使用vb.net创建一个vsto并使用以下代码为整个表着色

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

请帮忙

由于

Leroy M

2 个答案:

答案 0 :(得分:0)

这适用于VBA;我不使用.Net所以无法帮助。

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

答案 1 :(得分:0)

VB.NET中的第一个代码块改变了选择:

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

该块重新选择包含整个表格的形状,并且是选择的一部分 - 这会导致在代码开头选择整个形状和表格。

删除这段代码,剩下的就行了。