问题
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
回答1:
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
来源:https://stackoverflow.com/questions/20690375/programmatically-inserting-hidden-text-into-a-word-2010-table