繁体   English   中英

将 Excel 中的一系列单元格导出到 Word 并将 Word 文档保存为 Word 文件和 PDF 文件

[英]Exporting a range of cells in Excel to Word and saving the Word document as both a Word-file AND a PDF-file

我想要做的是:将 Excel 工作表中的一系列单元格导出为现有 Word 文档中的图像,然后将此 Word 文档另存为 Word 文档和 PDF 文件。 Word 文件和 PDF 文件应获得的名称位于 Excel 工作表的单元格中。

问题:除了 .pdf 文件外,几乎一切正常。 它已生成,但在尝试打开它时出现错误,说该文件不可读。

有人可以帮忙吗? 我使用的代码如下 - 我从这个论坛和其他论坛上的不同例子中组合起来(我真的是一个 VBA 初学者)......

非常感谢!

编码:

Sub SaveAsWord()

Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")

Dim WordDoc As Object
Set WordDoc = WordApp.Documents.Open("C:\Users\Jurgen\Documents\remake.docx")

Range("C4:E19").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
WordApp.Visible = True
WordApp.ActiveDocument.Bookmarks("here").Select
Set objSelection = WordApp.Selection
objSelection.Paste

Dim myfilename As String
myfilename = Sheets("Blad1").Range("G15")
WordApp.ActiveDocument.SaveAs2 Filename:="C:\Users\Jurgen\Documents\" & myfilename & ".pdf", _
FileFormat:=wdFormatPDF
WordApp.ActiveDocument.SaveAs2 Filename:="C:\Users\Jurgen\Documents\" & myfilename & ".docx", _
FileFormat:=wdFormatXMLDocument

End Sub

由于您不使用 Early Binding 并且您似乎也不使用Option Explicit ,根本原因是wdFormatPDF0 ,它应该是17

要么使用Option ExplicitEarly Binding要么只用这样的值替换常量

WordApp.ActiveDocument.SaveAs2 Filename:="C:\\Users\\Jurgen\\Documents\\" & myfilename & ".pdf", _ FileFormat:=17

WordApp.ActiveDocument.SaveAs2 Filename:="C:\\Users\\Jurgen\\Documents\\" & myfilename & ".docx", _ FileFormat:=12

阅读材料

选项显式

早期与晚期绑定

后期绑定

Microsoft 关于在自动化中使用早期绑定和后期绑定

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM