将副本保存为.PDF&.xlsx

我试图保存工作簿的副本,但不知道如何设置保存时的文件types,这段代码使文件,但他们已经损坏,无法打开。

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 

对于pdf:

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

对于xlsx:

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

希望这工作需要什么

一个简单的例子:

 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 

如果你想知道系统如何做到这一点,你可以注册macros。 女士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