AneeshS
AneeshS

Reputation: 63

Word VBA Code to select text in a cell, cut and paste special back into the same cell

I have a table with two columns and "x" number of rows.

In the 2nd column is formatted text which I would like to change to unformatted text.

The manual way of doing so is:

Select the whole cell in the 2nd column » Cut » Click Edit » Click Paste Special » Click Unformatted

The idea is to paste the unformatted text back into the cell it was Cut from and then move down to the cell below.

I would really appreciate some code that can apply this to all the cells in the 2nd column of a table.

Upvotes: 4

Views: 14395

Answers (1)

AneeshS
AneeshS

Reputation: 63

Here is the solution to my problem. A friend had a piece of code which I manipulated to suit my needs:

Sub CutAndPasteSpecialUnformatted()

        Dim value As Variable

        ' Process every row in the current table. '
        Dim row As Integer
        Dim rng As Range

        For row = 1 To Selection.Tables(1).Rows.Count
            ' Get the range for the rightmost cell. '
            Selection.Collapse Direction:=wdCollapseStart
            Set rng = Selection.Tables(1).Cell(row, Column:=2).Range

            ' For each, toggle text in rightmost cell. '
            rng.Select
            Selection.Copy
            Selection.Delete
            rng.Select
            Selection.Style = ActiveDocument.Styles("Normal")
            Selection.Delete
            Selection.Collapse Direction:=wdCollapseStart
            Selection.Range.PasteSpecial DataType:=wdPasteText
        Next

End Sub

Upvotes: 2

Related Questions