Word宏复制范围从Excel和粘贴到页面#

时间:2017-01-26 18:41:13

标签: excel vba ms-word word-vba

我使用word宏打开excel文件并复制范围,并将其粘贴为word文档中的表格。一切都很好,除了它粘贴到第1页而不是第6页(我需要它)。一个选项是更改段落编号,直到我找到第6页的方式,但是还有其他任何方式(我尝试了段号243,它发布在第5页的中间)。 WordCount中的段落编号并不对应。请提前协助并表示感谢。

Sub CopyExcelPasteWord()
    Dim objExcel As New Excel.Application
    Dim exWb As Excel.Workbook
    Dim tbl As Excel.Range
    Dim WordTable As Word.Table

    Set exWb = objExcel.Workbooks.Open("C:\Users\a222012\Desktop\Standard Bank\2017\FASS\CSS Project\Cash Shared Services\Fees_Contract.xlsm")
    Set tbl = exWb.Sheets("Device_Selection").Range("A5:G26")
    tbl.Copy
    Selection.GoTo wdGoToPage, wdGoToAbsolute, 6 'Go to page 6

    ThisDocument.Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False, _
      WordFormatting:=False, RTF:=False

    Set WordTable = ThisDocument.Tables(1)
    WordTable.AutoFitBehavior (wdAutoFitWindow)

    exWb.Close

    Set exWb = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

上述解决方案是在Word中使用书签。如果要从Excel中复制文本/表格或任何内容,请转到Word中的行,然后插入书签(并为其命名)。

然后使用以下代码粘贴到该书签:

Sub CopyExcelPasteWord()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Dim tbl As Excel.Range
Dim WordTable As Word.Table

Set exWb = objExcel.Workbooks.Open("Place path to file here including file name and extension")
Set tbl = exWb.Sheets("SheetNameHere").Range("RangeHere")
tbl.Copy


ThisDocument.Bookmarks("EnterBookmarkNameHere").Range.PasteExcelTable LinkedToExcel:=False, _
WordFormatting:=False, RTF:=False

'This next part just sets the table to fit across the page. The number in the bracket refers to the table 1 in the document. Change it accordingly.

Set WordTable = ThisDocument.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)

exWb.Close

Set exWb = Nothing
End Sub