我正在尝试保存工作簿的副本,但在保存时不知道如何设置文件类型,此代码会生成文件,但它们已损坏且无法打开。
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
答案 0 :(得分:2)
for 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
希望这项工作有用吗
答案 1 :(得分:1)
一个简单的例子:
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
答案 2 :(得分:0)
如果您想知道系统如何执行此操作,您可以注册宏。 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