Home > Net >  How can get a Range of Cell of Table in a Shape (VBA Word)?
How can get a Range of Cell of Table in a Shape (VBA Word)?

Time:06-20

I have a table put in a Shape of Word.

I want get a Range in Cell(2,1) at Text ="123"

enter image description here

I try this code , but it can't get a Range of Cell of Table in Shape:

Dim oApp As Object
Dim oShape As Object
Dim oTable As Object
Dim oDoc As Object

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

Set oDocument = oApp.Documents.Open("E:\2022\t1.docx")
Set oShape = oDocument.Shapes(1)
Set oTable = oShape.TextFrame.TextRange.Tables(1)

Dim iStart As Integer
Dim iEnd As Integer
iStart = oTable.Cell(2, 1).Range.Paragraphs(1).Range.Start
iEnd = iStart   3
oDocument.Range(iStart, iEnd).Text = "ABC"

Notes:

My code will working, if table put in document. But not working when table put in a Shape

How can get a Range of Cell of Table in a Shape?

CodePudding user response:

Assign the range of the cell to a variable first then use SetRange method to adjust the range before doing what you want:

Below will change the text of the first 3 characters in the Cell(2,1) 1st paragraph to "ABC":

Dim rng As Range
Set rng = oTable.Cell(2, 1).Range.Paragraphs(1).Range

rng.SetRange rng.Start, rng.Start   3
rng.Text = "ABC"
  • Related