导出文件名已存在的.xlsx和.pdf

时间:2019-07-10 05:05:30

标签: excel vba

长时间的监听器首次调用者。.需要一些BASIC帮助,这对于我的一生来说是我无法接受的。

保存.xlsx和PDF文件都在同一位置,只是无法让其提示是否已存在文件名

Sub SaveAs()
Dim Path As String
Dim filename As String
Path = "C:\Users\yard\Dropbox\Modus\Purchase Orders\"
filename = Range("E4")
            Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs filename:=Path & filename & ".xls", FileFormat:=xlOpenXMLWorkbook

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=Path & filename & ".pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

End Sub

还是我已经通过使用Application.DisplayAlerts = False取消了它?

在保存excel文件时,我不希望提示有关启用宏的工作簿,但是,我希望它提示文件名是否已经存在。.

有人能把我扔掉吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

查看代码中添加了“如果条件”的行:

Dim Path As String
Dim filename As String

Path = "C:\Users\yard\Dropbox\Modus\Purchase Orders\"

filename = Range("E4")

    Application.DisplayAlerts = False

    If Not Len(dir(Path & filename & ".xls")) = 0 Then MsgBox "Xls File Already Exist"
    ActiveWorkbook.SaveAs filename:=Path & filename & ".xls", FileFormat:=xlOpenXMLWorkbook

    If Not Len(dir(Path & filename & ".pdf")) = 0 Then MsgBox "PDF File Already Exist"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=Path & filename & ".pdf", Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
    Application.DisplayAlerts = True

End Sub

您可以使用Dir检查文件是否存在