[英]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
,根本原因是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
閱讀材料
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.