宏"保存为PDF"即使取消也可以保存

时间:2015-07-03 13:40:45

标签: excel vba pdf

在标题中,我遇到了一个宏问题。我想避免宏保存pdf文件,即使我按下Cancel对话框中的Save as。我错过了什么?

以下是代码:

Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler

Set ws = Foglio5

'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(Foglio5.Cells(14, 2) & "_" & (Foglio5.Cells(14, 4) & "_" & (Foglio5.Cells(15, 10))), "", ""), ".", "_") _
            & "_" _
            & Format(Foglio5.Cells(17, 5), "yyyymmdd\") _
            & ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile

myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

If myFile <> "False" Then
    ws.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

    MsgBox "PDF Creato! Si trova nella cartella di questo file."
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Errore nella creazione del PDF"
    Resume exitHandler
End Sub

1 个答案:

答案 0 :(得分:2)

更改行

If myFile <> "False" Then

If myFile Then

说明:

您声明(正确)myFileVariant。此类型将在必要时切换变量的实际类型。因此,在按下OK之后,例程返回类型为String的值(包含路径),并在按下时取消类型为Boolean的值(包含False)。