我想将表存储在变量中,cTable
并在需要时以所有格式将其粘贴。
Sub copytable()
Dim cTable As TABLE
Selection.Tables(1).Select
cTable = Selection.Tables ' how do i assign table into variable
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.Paste cTable ' how it going to be paste exacty the copied table
End Sub
示例在表格图片中:
@ken这是复制/粘贴表的简单代码,没有 variable
Selection.Tables(1).Select
Selection.COPY
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.PasteAndFormat (wdPasteDefault)
无法将表存储在变量中。可以使用变量来引用表,以便您始终可以引用它。
此代码示例演示了在引用表之前还是之后插入新表都无关紧要。如果它是文档中的第一个表,并且已被复制到文档的开头,则仍可以将其复制到文档的末尾(或其他位置)。
Sub ReuseTableReference()
Dim doc As word.Document
Dim tbl As word.Table
Dim rngTableTarget As word.Range
Set doc = ActiveDocument
Set tbl = doc.Tables(1)
Set rngTableTarget = doc.content
'Copy the table to the beginning of the document
rngTableTarget.Collapse wdCollapseStart
rngTableTarget.FormattedText = tbl.Range.FormattedText
'Copy the table to the end of the document
rngTableTarget.Start = doc.content.End
rngTableTarget.FormattedText = tbl.Range.FormattedText
'Copy the table to the current selection
Selection.FormattedText = tbl.Range.FormattedText
End Sub
当然,通常不希望对索引值进行硬编码。在这种情况下,可以将表添加为书签,以便您可以从书签中选择它,而不是:
Set tbl = doc.Bookmarks("tbl").Range.Tables(1)
(此处,索引值1表示加书签的Range内的表数。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句