简体   繁体   中英

VBA Word inputting into nested cells

Im looking to input text into nested cells in word using VBA. I can input into the top level table using the code below but can't input into a nested cell. (There are multiple nested tables).

Any help on how to do this?

ActiveDocument.Tables(1).Cell(Row:=23, Column:=19).Range.Text = ""

You have to test for nested cells and then point to the appropriate cell range. Here is an example.

If Selection.Cells(1).NestingLevel = 2 Then
    Selection.Cells(1).Range.Cells(1).Range.Text = "Nested"
Else
    Selection.Cells(1).Range.Text = "Not Nested"
End If

Using your example of a table that contains a nested table cell in Row 23, Column 19 the command will look like the following.

ActiveDocument.Tables(1).Cell(23, 19).Range.Cells(1).Range.Text = "123"

I want to emphasize though that you should test that there is actually a nested cell within the given cell range. If you don't, your code could fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM