user1173805
user1173805

Reputation: 79

Copy Text from Table in Word and Retaing Formatting

I have a problem copying a cell from a table to another cell. I'm talking about two word documents here. I can copy the text but the bullets are gone and some of the formatting.

I tried .Formattedtext but still can't do it.

Dim test As Word.Cell

'An error occurs something like "Object variable or With block variable not set"
test.Range.FormattedText = CTPDoc.Tables(2).Rows(testCount).Cells(3).Range.FormattedText

Upvotes: 1

Views: 13200

Answers (2)

user1173805
user1173805

Reputation: 79

@Siddharth Rout You answer was really helpful. It's not the exact answer to my problem but at least I learned about PasteandFormat and its different types such as wdFormatOriginalFormatting. maybe someday I can use that.

Now here goes what solved my problem. Using the logic given by Siddharth, I used the simple tbl2.Cell(1, 1).Range.Paste instead of PasteandFormat. Actually PasteandFormat worked but there was a problem which happens in only selected source file/table. I think there's some formatting that exist in the source table that when pasted in another cell, it'll look messed up. I'm not sure about what exactly what's that but .Paste definitely solved it for me. I hoped I can help others too :)

Upvotes: 1

Siddharth Rout
Siddharth Rout

Reputation: 149287

Here is an example.

Let's say we have two tables in a word document. See screenshot below

enter image description here

Let's say we want to paste the data from Cell 1 of Table 1 to Cell 1 of Table 2 then try this

Sub Sample()
    Dim tbl1 As Table, tbl2 As Table

    Set tbl1 = ActiveDocument.Tables(1)
    Set tbl2 = ActiveDocument.Tables(2)

    tbl1.Cell(1, 1).Range.Copy
    tbl2.Cell(1, 1).Range.PasteAndFormat (wdFormatOriginalFormatting)
End Sub

This is what the macro does

enter image description here

Hope this helps :)

Upvotes: 4

Related Questions