【问题标题】:Macro "Save to PDF" saves even on cancels宏“保存为 PDF”即使在取消时也会保存
【发布时间】:2015-07-03 13:40:44
【问题描述】:

如标题所示,我对宏有疑问。即使我在Save as 对话框中按下Cancel,我也想避免宏保存pdf文件。我错过了什么?

代码如下:

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

【问题讨论】:

    标签: excel vba pdf


    【解决方案1】:

    换行

    If myFile <> "False" Then
    

    进入

    If myFile Then
    

    解释:

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

    【讨论】:

    • 成功了,非常感谢!感谢您的解释,它非常有用:)
    猜你喜欢
    • 2015-09-23
    • 2012-03-07
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    相关资源
    最近更新 更多