使用变量复制粘贴 Table
Copy Paste Table using Variable
我想将 table 存储在变量 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
示例在 table 图片中:
@ken 这是复制/粘贴的简单代码 table 没有 variable
Selection.Tables(1).Select
Selection.COPY
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.PasteAndFormat (wdPasteDefault)
无法将 table 存储在 变量中。可以使用一个变量来引用一个table,这样你就可以随时引用它。
此代码示例演示了在引用的 table 之前或之后插入新的 table 并不重要。如果它是文档中的第一个 table 并且它被复制到文档的开头,它仍然可以被复制到文档的末尾(或其他任何地方)。
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
当然,索引值中的硬编码通常是不可取的。在这种情况下,可以将 table 添加为书签,以便您可以从书签中提取它,而不是:
Set tbl = doc.Bookmarks("tbl").Range.Tables(1)
(这里索引值1指的是书签范围内的table个数。)
我想将 table 存储在变量 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
示例在 table 图片中:
@ken 这是复制/粘贴的简单代码 table 没有 variable
Selection.Tables(1).Select
Selection.COPY
Selection.MoveDown Unit:=wdLine, Count:=2
Selection.PasteAndFormat (wdPasteDefault)
无法将 table 存储在 变量中。可以使用一个变量来引用一个table,这样你就可以随时引用它。
此代码示例演示了在引用的 table 之前或之后插入新的 table 并不重要。如果它是文档中的第一个 table 并且它被复制到文档的开头,它仍然可以被复制到文档的末尾(或其他任何地方)。
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
当然,索引值中的硬编码通常是不可取的。在这种情况下,可以将 table 添加为书签,以便您可以从书签中提取它,而不是:
Set tbl = doc.Bookmarks("tbl").Range.Tables(1)
(这里索引值1指的是书签范围内的table个数。)