Copy Text from Table in Word and Retaing Formatting

流过昼夜 提交于 2020-01-05 11:58:24

问题


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

回答1:


Here is an example.

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

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

Hope this helps :)




回答2:


@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 :)



来源:https://stackoverflow.com/questions/14316916/copy-text-from-table-in-word-and-retaing-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!