Copy text from spreadsheet to Word and Retaing formatting

I have a problem copying a cell from a table to another cell. I am talking about two documents. I can copy the text, but the bullets disappeared and some 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
vba ms-word word-vba
source share
2 answers

Here is an example.

Say we have two tables in a text document. See screenshot below.

enter image description here

Say we want to insert data from Cell 1 from Table 1 into Cell 1 from 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 a macro.

enter image description here

Hope this helps :)

+3
source share

@Siddharth Rout Your answer was really helpful. This is not an exact answer to my problem, but at least I found out about PasteandFormat and its various types, such as wdFormatOriginalFormatting . maybe someday I can use this.

Now here is what solved my problem. Using the logic given by Siddhart, I used the simple tbl2.Cell(1, 1).Range.Paste instead of PasteandFormat . Actually PasteandFormat worked, but there was a problem that occurs only in the selected source file / table. I think that in the original table there is some formatting that when pasted into another cell will look corrupted. I'm not sure what exactly this is, but .Paste definitely decided this for me. I was hoping I could help others too :)

+1
source share

All Articles