将 Excel 工作表复制到 Word

发布于 2024-10-30 17:08:31 字数 853 浏览 1 评论 0原文

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

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

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

迈克的回答编辑:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花桑 2024-11-06 17:08:31

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

expression.PasteExcelTable(LinkedToExcel, WordFormatting, RTF)

尝试其中之一

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

祝您好运 - MikeD

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文