我有一个 VBA 宏,它将 excel 文件保存到与其所在的同一目录中。它运行良好,符合预期。
我想要做的是将文件保存为 PDF。
我尝试了各种代码,但由于我不理解这些东西,所以失败了。这是将文件保存为 .xlsm 的代码
Sub save_file()
Dim path As String
Dim filename1 As String
path = ThisWorkbook.path & "\" 'Same path as current project that the User opened.
filename1 = Range("M1").Text
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs filename:=path & filename1 & ".xlsm"
Application.DisplayAlerts = True
End Sub
据我所知,这并不像改变那么.xlsm
简单.PDF
。
答案1
我想要做的是将文件保存为 PDF
使用下列方法之一:
Sub SaveActiveSheetsAsPDF()
'Create and assign variables
Dim saveLocation As String
saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"
'Save Active Sheet(s) as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation
End Sub
Sub SaveActiveWorkbookAsPDF()
'Create and assign variables
Dim saveLocation As String
saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"
'Save active workbook as PDF
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation
End Sub