我要做的事情:将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发布于 2020-01-08 14:32:50
由于您不使用早期绑定,而且似乎也不使用Option Explicit,根本原因将是wdFormatPDF是0,而应该是17。
要么使用Option Explicit和Early 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
阅读材料
https://stackoverflow.com/questions/59647749
复制相似问题