【问题标题】:Determine if SaveAs FileFormat is PDF确定 SaveAs FileFormat 是否为 PDF
【发布时间】:2018-01-31 11:27:55
【问题描述】:

我想在 Excel 工作表保存为 PDF 之前对其进行更改,并在创建副本后撤消这些更改。我想使用Workbook_BeforeSave 事件来实现更改,并使用Workbook_AfterSave 事件将工作表返回到其原始状态。

当我想确定 SaveAs 指令的输出格式时,我遇到了困难。它似乎在 Workbook_BeforeSave 事件中不可用,而且我知道没有任何事件可以捕获 SaveAs 对话框,除非我强制用户使用此对话框的特定实例。

我希望用户从 Excel 的 UI 中选择 SaveAs 并在他选择另存为 PDF 时采取行动。

【问题讨论】:

    标签: vba excel pdf


    【解决方案1】:

    您可以通过控制msoFileDialogSaveAs 来确定文件路径和扩展名,如下所示:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
        Dim intChoice As Integer
        Dim strPath As String
    
        'make the file dialog visible to the user
        intChoice = Application.FileDialog(msoFileDialogSaveAs).Show
    
        'determine what choice the user made
        If intChoice <> 0 Then
            'get the file path selected by the user
            strPath = _
            Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
            'displays the result in a message box
            Call MsgBox(strPath, vbInformation, "Save Path")
        End If
    
        SaveAsUI = False
        Cancel = True
    
    End Sub
    

    获得完整路径后,Cancel = True 将停止 SaveAs 操作,因此您可以选择是否保存。

    编辑:您确定用户是否按下了Save 按钮或SaveAsSaveAsUI。如果按下Save,则按下SaveAsUI = False,如果按下SaveAs,则按下SaveAsUI = True

    编辑 2: 这是我将如何实现上述内容的示例:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
        'cancel all default actions by excel application
        Cancel = True
    
        'if save was selected instead of SaveAs
        If (SaveAsUI = False) Then
            Cancel = False
    
        'if SaveAs was selected
        ElseIf (SaveAsUI = True) Then
    
            SaveAsUI = False
            Dim intChoice As Integer
            Dim strPath As String
    
            'make the file dialog visible to the user
            intChoice = Application.FileDialog(msoFileDialogSaveAs).Show
    
            'determine what choice the user made
            If intChoice <> 0 Then
                'get the file path selected by the user
                strPath = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
    
                'get file extention
                fileExt = Right(strPath, Len(strPath) - InStr(strPath, ".") + 1)
    
                'if pdf is the chosen format
                If (InStr(strPath, "PDF") > 0) Then
    
                    'actions with pdf format
                    Call MsgBox(strPath, vbInformation, "Save Path")
                Else
    
                    With ActiveWorkbook
                        'on error resume next in case of extention macro free prompt no is selected
                        'see example by removing on error resume next and saving extention ".xlsx"
                        On Error Resume Next
                        .SaveAs strPath
                        On Error GoTo 0
                    End With
    
                End If
            End If
    
        End If
    End Sub
    

    【讨论】:

    • SaveAs 操作由用户调用 SaveAs 对话框启动。然后,您的解决方案将为他提供另一个 SaveAs 对话框。请阅读我的问题的最后一句话。你提出了一种我明确想要避免的方式。
    • @Variatus,如果您试图阻止为用户提供两个SaveAs File Dialog 提示,您只需打开SaveAsUI = False 并阻止原始SaveAs action。如果用户只尝试Save,为了防止File Dialog,用SaveAsUI = False 包装它,这是UI 的默认保存事件状态。当您可以控制过程时,尝试确定未发生的事情的输出似乎是错误的选择,例如,您不再需要AfterSave 来恢复文件,而是创建一个新文件以另存为 PDF。如果不是 .PDF,只需按预期保存即可。
    • 感谢您的努力。我已对您的回复投了赞成票,但无法接受您的回复。同样,顺序如下:用户单击 SaveAs 并选择 PDF(或不选择)。我想知道他选择了什么。撤消 SaveAs 操作不会将对话框放回框中,因为它已经显示。另一种方法是给他一个按钮来调用 SaveAs,然后一切都很容易。我现在已经辞职了,但这不是我所希望的。
    • @Variatus,我添加了一个如何实现它的示例,我认为它涵盖了您需要的大部分内容。但无论如何祝你好运^^
    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2023-03-11
    • 2011-02-16
    相关资源
    最近更新 更多