我在 Excel 表中添加了一个按钮,现在当我单击该按钮时,它会将表以特定名称保存在特定路径下,但以 pdf 格式保存。
我只想以 excel 格式 (.xlsx) 保存此工作表。因此
Sub PDFActiveSheet2()
Dim ws As Worksheet
Dim strFile As String
On Error GoTo errHandler
strFile = "m:\formats\" & Range("H8")
Set ws = ActiveSheet
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
MsgBox "file has been created."
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create the file"
Resume exitHandler
End Sub
我需要改变什么?
答案1
更改以下代码
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
到下面的代码
ActiveWorkbook.SaveAs Filename:="C:\Users\46506090\Desktop\Book1.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
更改路径(C:\用户\46506090\桌面\Book1.xlsm) 到您想要的并查看它是否有效。
CreateBackup:=False
这是可选的
编辑:
完整代码
Option Explicit
Sub Button1_Click()
'Sub PDFActiveSheet2()
Dim ws As Worksheet
Dim strFile As String
On Error GoTo errHandler
strFile = "m:\formats\" & Range("H8")
Set ws = ActiveSheet
ActiveWorkbook.SaveAs Filename:="C:\Users\46506090\Desktop\Book1.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
'Comment: Replace "C:\Users\46506090\Desktop\Book1.xlsm" to your desired filename
MsgBox "file has been created."
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create the file"
Resume exitHandler
End Sub
答案2
更正其他答案应该只有一个保存位置
Option Explicit
Sub Button1_Click()
'Sub PDFActiveSheet2()
Dim ws As Worksheet
Dim strFile As String
On Error GoTo errHandler
strFile = "C:\Users\yourName\Desktop\Book1.xlsm"
'Comment: Replace "C:\Users\yourName\Desktop\Book1.xlsm" to your desired filename
Set ws = ActiveSheet
ActiveWorkbook.SaveAs Filename:=strFile, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
MsgBox "file has been created."
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create the file"
Resume exitHandler
End Sub