【问题标题】:Warning message if file name already exists when creating a pdf file如果在创建 pdf 文件时文件名已经存在,则会出现警告消息
【发布时间】:2016-08-09 06:40:25
【问题描述】:

我使用以下宏从我的 Excel 电子表格创建一个 PDF 文件:

Sub PDF_01()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & "test.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub

到目前为止,此宏运行良好。但是,一旦我创建了文件“test.pdf”并再次运行宏,它将覆盖第一个文件,而不会给出任何警告消息,例如“文件名已经存在。你想覆盖它吗?”。

您知道如何在我的代码中包含此消息吗?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以使用Dir查看文件是否已经存在,然后给用户一个替代选择,即

    Dim StrIn As String
    StrIn = ThisWorkbook.Path & "\" & "test.pdf"
    
    If Len(Dir(StrIn)) = 0 Then
        ActiveSheet.ExportAsFixedFormat xlTypePDF, Filename:=StrIn, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    Else
        MsgBox "file already exists"
        ' do something else
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 2012-08-24
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多