VBA-Table Word 2019 - Move a selected text, from a cell to the adjacent cell to its right, by pressing the ENTER key

cdl

Joined
Feb 14, 2022
Messages
2
Reaction score
0
Hello,
I have a table with 4 rows over 3 columns.
This VBA code should move a selected text, from one cell to the adjacent cell to its right, after selecting the ENTER key.
I tried it, but it doesn't work. Am I wrong or missing something?

Sub MoveTextToRight()
Dim oTable As Table
Dim oCell as cell
Dim oSelection as a selection

Set oSelection = Application.Selection

If oSelection.Information(wdWithInTable) = True then
Set oTable = oSelection.Tables(1)
Set oCell = oSelection.Cells(1)

If not oCell.Next is nothing, then
oSelection.Cut
oCell.Next.Range.InsertBefore Selection.Text
Else
MsgBox "The text cannot be shifted to the right, as there is no adjacent cell."
End If
Else
MsgBox "Select a cell within a table".
End If
End sub

I don't have to move the contents of a cell, just what is selected in a cell.
Example:
in a table cell, I have this sentence: Check inserted files.
I would like that, by selecting inserted files and pressing the ENTER key, the two selected words were moved to the adjacent cell on the right.
To run the code, I insert this into the VB page with the Develop menu (ALT+F11).
Carlo-Italy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top