强制模板保存为xlsm,但之后允许保存为pdf

我已经创build了一个xltm文档供我的工作场所使用。 大多数用户将xls设置为默认保存选项,因此当用户在configuration完成后保存该文档时,必须强制将其保存为xlsm,否则将失去function。 为了覆盖这个,我已经使用了下面的代码(从其他地方借用和修改别人的问题):

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim vFilename As Variant 'Disable events so "new save" doesn't re-trigger this event Application.EnableEvents = False If SaveAsUI = True Then ' User selected SaveAs instead of Save Cancel = True ' Cancel the user's original save action 'Application.DisplayAlerts = False 'Simulate built in "SaveAs", Use desired "IntialFilename" & filter vFilename = Application.GetSaveAsFilename(InitialFileName:="C:\Temp\Development.xlsm", fileFilter:="Excel Files (*.xlsm), (*.xlsm") If vFilename <> False Then 'Save file with desired parameters ThisWorkbook.SaveAs Filename:=vFilename, FileFormat:=xlOpenXMLWorkbookMacroEnabled End If Debug.Print vFilename Application.DisplayAlerts = True End If 'Re-enable events Application.EnableEvents = True End Sub 

这按预期工作,迫使用户在第一次使用模板之后保存为xlsm。 但是,由于代码保留在工作表中,这意味着新configuration的工作performance在永远不会被保存为xlsm以外的任何其他用途。

这是有问题的,因为在某一点上,用户很可能希望将工作簿中的单个工作表保存为PDF,以向他们提供他们不想访问实际工作簿及其基础公式的人。

有什么办法可以导致上面的代码从用户初始保存到xlsm后从文件中删除? 或者添加上面的代码也可以保存为PDF文件格式?

我意识到一个解决方法是人们使用诸如CutePDF的“PDF打印机”来保存文件,而不是使用内置的保存function,但这不适合我的工作场所的IT能力水平。

任何尝试回答非常赞赏。

选项1 :embedded一个新的macros/button,将导出到PDF代码。 这仍然会强制所有手动保存符合.xlsm限制。

 Sub ExportToPDF() Application.EnableEvents = False 'Prevent the saveas restriction from running 'Get filename for saving PDF Dim vFilename As Variant vFilename = Application.GetSaveAsFilename(InitialFileName:="C:\Temp\Development.pdf", fileFilter:="PDF (*.pdf), *.pdf") If vFilename <> False Then 'Export PDF with selected filename ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vFilename, _ Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _ OpenAfterPublish:=False End If Application.EnableEvents = True 'Reset event handler End Sub 

选项2 :修改您的BeforeSave子程序以允许.xlsm或.pdf保存。

 Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim vFilename As Variant 'Disable events so "new save" doesn't re-trigger this event Application.EnableEvents = False If SaveAsUI = True Then ' User selected SaveAs instead of Save Cancel = True ' Cancel the user's original save action 'Application.DisplayAlerts = False 'Simulate built in "SaveAs", Use desired "IntialFilename" & filter vFilename = Application.GetSaveAsFilename(InitialFileName:="C:\Temp\Development.xlsm", fileFilter:="All Files (*.*), *.*", Title:="Save As: XLSM or PDF Only!") ' If vFilename <> False Then 'Check to see if user is trying to save as .xlsm, .pdf, or other If Right(vFilename, 5) = ".xlsm" Then 'Save file with desired parameters ThisWorkbook.SaveAs Filename:=vFilename, FileFormat:=xlOpenXMLWorkbookMacroEnabled ElseIf Right(vFilename, 4) = ".pdf" Then 'Export PDF with selected filename ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=vFilename, _ Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _ OpenAfterPublish:=False Else 'Alert user and do not save MsgBox "This workbook can only be saved in .xlsm or .pdf formats. Please try again." End If End If Debug.Print vFilename Application.DisplayAlerts = True End If 'Re-enable events Application.EnableEvents = True End Sub