copy Excel sheet to Word

青春壹個敷衍的年華 提交于 2019-12-22 11:20:56

问题


I need to export the sheet 'GreatIdea' to a Word document. 'GreatIdea' is divided into pages and my columns are based on these pages.

A - C contain a table of contents, D - F contain chapter 1, ...

Sub test()

' Open LOL.docx'
Dim appWD As Word.Application
Set appWD = New Word.Application
Dim docWD As Word.Document
Set docWD = appWD.Documents.Open("C:\Users\TOM\Desktop\LOL.docx")
docWD.Activate
Sheets("Sheet1").Select

' Copy from GreatIdea to LOL.docx'

Range("A1:K40").Copy
appWD.Selection.PasteSpecial

appWD.ActiveDocument.SaveAs Filename:=ThisWorkbook.Path & "/" & "OEF_OFFERTE"
appWD.ActiveDocument.Close
appWD.Quit
Set appWD = Nothing
Set docWD = Nothing

End Sub

This copies everything into Word, but doesn't copy the column layout. Other solutions to copy everything are accepted too. I just need to make sure all the data from every column gets copied.

Mike's answer edited:

 Range("A1:C40").Copy
 appWD.Selection.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=True

回答1:


As you are in a WORD application (AppWD), there's a better function:

expression.PasteExcelTable(LinkedToExcel, WordFormatting, RTF)

Try one of these

AppWD.Selection.PasteExcelTable False, True, True   ' aequivalent to PasteSpecial As RTF
AppWD.Selection.PasteExcelTable False, False, True  ' keeps Excel formats
AppWD.Selection.PasteExcelTable False, False, False ' aequivalent to PasteSpecial As HTML

Good luck - MikeD



来源:https://stackoverflow.com/questions/5537362/copy-excel-sheet-to-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!