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

时间:2016-02-22 17:28:23

标签: excel vba excel-vba pdf

我已经创建了一个xltm文档,可以在我的工作场所使用。大多数用户将xls设置为默认保存选项,因此当人们在配置后保存此文档时,他们需要被强制保存为xlsm,否则它们将失去功能。为了覆盖这一点,我使用了以下代码(借用并改编自别人的其他地方的问题):

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。但是,由于代码保留在工作表中,这意味着新配置的工作表现在永远不会在整个生命周期和使用期间保存为除xlsm以外的任何其他工作表。

这是有问题的,因为在某些时候,用户很可能希望将单个工作簿保存在工作簿中,以便向他们提供给他们而不是没有的人访问实际工作簿及其基础公式。

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

我意识到一种解决方法是让人们使用&#34; PDF打印机&#34;比如CutePDF来保存文件而不是内置的保存功能,但这不适合我工作场所的IT能力水平。

任何对答案的尝试都非常感激。

1 个答案:

答案 0 :(得分:0)

选项1 :嵌入一个新的宏/按钮,该代码将导出为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