简体   繁体   English

确定SaveAs FileFormat是否为PDF

[英]Determine if SaveAs FileFormat is PDF

I want to make changes to an Excel worksheet before it is saved to PDF and undo those changes after the copy has been created. 我想在将Excel工作表保存为PDF之前对其进行更改,并在创建副本后撤消那些更改。 I thought of using the Workbook_BeforeSave event to implement the changes and the Workbook_AfterSave event to return the worksheet to its original state. 我想到了使用Workbook_BeforeSave事件来实现更改,并使用Workbook_AfterSave事件来使工作表返回其原始状态。

The point at which I got stuck is when I want to determine the output format for the SaveAs instruction. 当我想确定SaveAs指令的输出格式时,就会遇到麻烦。 It doesn't seem to be available in the Workbook_BeforeSave event, and I know of no event that I might capture the SaveAs dialog with unless I force the user to use a particular instance of this dialog. 它似乎在Workbook_BeforeSave事件中不可用,而且我不知道可能会捕获SaveAs对话框的事件,除非我强迫用户使用该对话框的特定实例。

I would like the user to choose SaveAs from Excel's UI and take action if he chose to save as PDF. 我希望用户从Excel的UI中选择“另存为”,如果他选择另存为PDF,请采取措施。

You can determine the file path and extension by controlling the msoFileDialogSaveAs like this: 您可以通过控制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

After you have the full path Cancel = True will stop the SaveAs action so you can choose to save or not. 拥有完整路径后, Cancel = True将停止SaveAs操作,因此您可以选择是否保存。

Edit: You determine if the user pressed Save button or SaveAs with SaveAsUI . 编辑:您判断,当用户按下Save按钮或SaveAsSaveAsUI If Save pressed then SaveAsUI = False if SaveAs then SaveAsUI = True 如果按下“ Save ,则SaveAsUI = False如果SaveAsSaveAsUI = True

Edit 2: This is an example of how I would implement the above: 编辑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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM