programmatically inserting hidden text into a Word 2010 table

前提是你 提交于 2020-01-05 12:30:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!