2015-07-03 97 views
1

与标题一样,我遇到了宏问题。我想避免该宏保存PDF文件,即使我在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 

回答

2

行更改

If myFile <> "False" Then 

If myFile Then 

说明:

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

+0

它的工作,非常感谢!感谢您的解释,这非常有用:) –