2013-01-14 115 views
0

我在将表格中的单元格复制到另一个单元格时遇到问题。我在这里谈论两个单词文件。我可以复制文本,但子弹消失了,还有一些格式化。我试过.Formattedtext但仍不能做到。从Word中复制表格并保留格式

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 
+0

你试过'Selection.PasteAndFormat(wdFormatOriginalFormatting)'? –

+0

我还没有这样做。我如何使用它?对不起,我是VBA的新手 – user1173805

+0

你能证明你尝试过什么吗? –

回答

1

这里是一个例子。

假设我们在word文档中有两个表格。见下面

enter image description here

截图比方说,我们希望从Table 1Cell 1数据粘贴到的Table 2Cell 1那就试试这个

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 

这是宏做什么

enter image description here

希望这个帮助:)

+0

I我想我可以在我的场景中使用它。这两个表格来自不同的文件。我的问题是我打开目标文件并粘贴所有值之前先获取所有值。谢谢 – user1173805

+0

你上面的代码表明你正在读取表格的值,所以我上面提出的方法将适用于你。试试吧 –

+0

它工作!非常感谢你!现在我在第一份工作中做了一些很好的工作:D – user1173805

0

@Siddharth Rout 你的回答是非常有帮助的。这不是我的问题的确切答案,但至少我了解了pasteandFormat及其diffrenet类型,如{wdFormatOriginalFormatting}。也许有一天我不能使用它。

现在,这里解决了我的问题。 使用Siddharth给出的逻辑,我使用了简单的{tbl2.Cell(1,1).Range.Paste}而不是PasteandFormat。其实PasteandFormat工作,但有一个问题发生在只有选定的源文件/表。我认为源表中存在一些格式,当粘贴到另一个单元格时,它会看起来很乱。我不确定究竟是什么,但是{.Paste}肯定为我解决了这个问题。我希望我可以帮助别人:)

相关问题