Excel “宏”;“保存到PDF”;即使取消也可以节省

Excel “宏”;“保存到PDF”;即使取消也可以节省,excel,vba,pdf,Excel,Vba,Pdf,正如在标题中一样,我对宏有一个问题。我希望避免宏保存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

正如在标题中一样,我对宏有一个问题。我希望避免宏保存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
Sub-PDFActiveSheet()
将ws设置为工作表
将strPath设置为字符串
Dim myFile作为变量
作为字符串的Dim strFile
关于错误转到错误处理程序
设置ws=Foglio5
'输入名称并选择文件的文件夹
'在当前工作簿文件夹中启动
strFile=Replace(Replace(Foglio5.Cells(14,2)&“Foglio5.Cells(14,4)&”和“Foglio5.Cells(15,10)),“,”,“,”,“,”_
& "_" _
&格式(Foglio5.Cells(17,5),“yyyyymmdd\”)_
&“.pdf”
strFile=thishworkbook.Path&“\”strFile
myFile=Application.GetSaveAsFilename_
(InitialFileName:=strFile_
FileFilter:=“PDF文件(*.PDF),*.PDF”_
标题:=“选择要保存的文件夹和文件名”)
如果myFile为“False”,则
ws.ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=myFile_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=False
MsgBox“PDF Creato!Si trova nella cartella di questo文件。”
如果结束
出口商:
出口接头
错误处理程序:
MsgBox“Errore nella creazione del PDF”
复出机
端接头
更改行

If myFile <> "False" Then

说明:


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

它成功了,非常感谢!谢谢你的解释,非常有用:)
If myFile Then