vba 将 Excel 工作表复制到 Word

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5537362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:56:45  来源:igfitidea点击:

copy Excel sheet to Word

excelexcel-vbams-wordcopypastevba

提问by CustomX

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

我需要将工作表“GreatIdea”导出到 Word 文档。“GreatIdea”分为几页,我的专栏基于这些页。

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

A - C 包含目录,D - F 包含第 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.

这会将所有内容复制到 Word 中,但不会复制列布局。其他复制所有内容的解决方案也被接受。我只需要确保每一列的所有数据都被复制。

Mike's answer edited:

迈克的回答编辑:

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

采纳答案by MikeD

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

当您在 WORD 应用程序 (AppWD) 中时,有一个更好的功能:

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

祝你好运 - MikeD