以编程方式将隐藏文本插入Word 2010表

时间:2013-12-19 19:46:26

标签: vba ms-office word-vba

我有一个Word 2010表,其中包含现有的可见文本。我想使用一些VBA代码将一些文本插入每个单元格,然后隐藏新文本。我知道如何使用VBA将文本插入单元格中,我只是无法弄清楚如何将单元格中的现有文本保持可见并且只隐藏新文本。

我试过这个,但它不太有效:

For Each aTable In ActiveDocument.Tables

Rows = aTable.Rows.Count 
Cols = aTable.Columns.Count 
Dim rng As Range 
For r = 1 To Rows 
For c = 1 To Cols 
cellvalue = "Cell_ID[" & r & ", " & c & "]" 
ActiveDocument.Tables(ndx).Cell(r, c).Range.InsertAfter cellvalue 
' hides all text in the cell
'ActiveDocument.Tables(ndx).Cell(r, c).Range.Font.Hidden = True  
Selection.Font.Hidden = True 
Next 
Next 
Exit For 

Next aTable

1 个答案:

答案 0 :(得分:0)

Sub Tester()

Dim rng As Range, aTable, nRows, nCols, r, c, cellvalue
Dim l

    For Each aTable In ActiveDocument.Tables

    nRows = aTable.Rows.Count
    nCols = aTable.Columns.Count

        For r = 1 To nRows
            For c = 1 To nCols
                cellvalue = "Cell_ID[" & r & ", " & c & "]"
                With aTable.Cell(r, c)
                    l = Len(.Range.Text)
                    .Range.InsertAfter cellvalue
                    Set rng = .Range
                    rng.MoveStart wdCharacter, l - 2
                    rng.Font.Hidden = True
                End With
            Next c
        Next

    Next aTable

End Sub