如果在创buildPDF文件时文件名已经存在,则会显示警告消息

我使用下面的macros从我的Excel电子表格创build一个PDF文件:

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

这个macros到目前为止工作得很好。 但是,一旦我创build了“test.pdf”文件,并且再次运行macros,它将覆盖第一个文件,而不会给出任何警告消息,如“文件名已存在,是否要覆盖它?”。

你知道我怎么可以在我的代码中包含这个消息吗?

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

 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