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

问题描述:

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

I have a Word 2010 table with existing, visible text in it. I want to use some VBA code to insert some text into each cell and then hide the new text. I know how to insert text into a cell using VBA, I just can't figure out how to leave the existing text in the cell visible and only hide the new text.

我尝试了这个,但效果不佳:

I tried this, but it doesn't quite work:

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

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