简体   繁体   English

将副本另存为.PDF和.xlsx

[英]Save copies as .PDF & .xlsx

I'm trying to save copies of the workbook but don't know how to set the file type when saving, this code makes the files but they're corrupt and cannot be opened. 我正在尝试保存工作簿的副本,但是在保存时不知道如何设置文件类型,此代码可以创建文件,但它们已损坏且无法打开。

Sub Saves1()

'Store Answers
Dim SavePdfAnswer As String
Dim SaveXlsxAnswer As String
SavePdfAnswer = VBA_CS.Range("C2")
SaveXlsxAnswer = VBA_CS.Range("C3")

'Store File Path And Names
PdfFilePath = VBA_CS.Range("M2") & "\" & ActiveSheet.Range("F9") & ".pdf" 'File path for pdf file
ExcelFilePath = VBA_CS.Range("M2") & "\" & ActiveSheet.Range("F9") & ".xlsx" 'File path for excel xlsx file

'Save as pdf
If SavePdfAnswer = "Yes" Then
  ActiveWorkbook.SaveCopyAs PdfFilePath
End If

'Save as excel xlsx
If SaveXlsxAnswer = "Yes" Then
  ActiveWorkbook.SaveCopyAs ExcelFilePath
End If

End Sub

for pdf: 对于pdf:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="path\pdf_from_excel.pdf" _
    , Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True

for xlsx: 对于xlsx:

ActiveWorkbook.SaveAs Filename:= _
    "path\excel_file_name.xlsx" _
    , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

Hope this works whats needed 希望这行得通

A quick example: 一个简单的例子:

Sub SaveFile()
Dim tmpPth As String

On Error GoTo errorhandle

tmpPth = FilePath & FileName


ThisWorkbook.Sheets("Sheetname").ExportAsFixedFormat Type:=xlTypePDF, FileName:=tmpPth & ".pdf", Quality:=xlQualityStandard, openAfterPublish:=False

ActiveWorkbook.SaveCopyAs tmpPth & ".xlsm"

Exit Sub

errorhandle:
MsgBox ("Something went wrong")
End Sub

if You want to know how system do this you can register macros. 如果您想知道系统如何执行此操作,可以注册宏。 Ms Word generated this code : Word女士生成了以下代码:

 ActiveDocument.ExportAsFixedFormat OutputFileName:= _ "C:\\Users\\Administrator\\Desktop\\fileName.pdf", ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _ wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _ IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _ wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _ True, UseISO19005_1:=False 

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

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