【问题标题】:Getting the "Do you want to overwrite the file" dialog box to show when saving with VBA使用 VBA 保存时显示“是否要覆盖文件”对话框
【发布时间】:2015-06-29 11:40:32
【问题描述】:

以下代码保存了我的 Excel 工作表的选定区域。但是,如果我尝试保存与已经存在的文件具有相同文件名的文件,它只会保存文件而不显示“您要覆盖文件”对话框。

有没有办法更改此代码,以便它会询问我是否要覆盖预先存在的文件?

Option Explicit
Sub CreatePDF()
Dim wSheet As Worksheet
Dim vFile As Variant
Dim sFile As String

Set wSheet = ActiveSheet
sFile = Replace(Replace(wSheet.Name, " ", ""), ".", "_") _
        & "_" _
        & Format(Now(), "yyyymmdd\_hhmm") _
        & ".pdf"
sFile = ThisWorkbook.Path & "\" & sFile

vFile = Application.GetSaveAsFilename _
(InitialFileName:=sFile, _
    FileFilter:="PDF Files (*.pdf), *.pdf", _
    Title:="Select Folder and FileName to save")

If vFile <> "False" Then
wSheet.Range("B2:J44").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    FileName:=vFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

MsgBox "PDF file has been created."
End If
End Sub

【问题讨论】:

  • 您可以使用Dir() 来检查文件是否存在,然后使用MsgBox 弹出您自己的消息。

标签: vba excel


【解决方案1】:

按照建议,一种模拟行为的方法是检查选定的 SaveAsFilename:

Option Explicit

Sub CreatePDF()
    Dim wSheet As Worksheet
    Dim vFile As Variant
    Dim sFile As String

    Set wSheet = ActiveSheet
    sFile = Replace(Replace(wSheet.Name, " ", ""), ".", "_") _
            & "_" _
            & Format(Now(), "yyyymmdd\_hhmm") _
            & ".pdf"
    sFile = ThisWorkbook.Path & "\" & sFile

    vFile = Application.GetSaveAsFilename _
    (InitialFileName:=sFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

    If Dir(vFile) > vbNullString Then _
        If MsgBox("Overwrite File?", _
                   vbExclamation + vbYesNo, "Overwrite?") = vbNo Then Exit Sub

    If vFile <> "False" Then
        wSheet.Range("B2:J44").ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=vFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False

        MsgBox "PDF file has been created."
    End If
End Sub

【讨论】:

  • 这个方法有效,它做我想做的事,但现在如果我取消保存代码错误
  • 您能否提供任何错误详细信息,以及错误所在的代码行(当您单击“调试”按钮时,它将带您到该行)
  • 返回的错误是:“运行时错误'5':无效的过程调用或参数单击调试时,突出显示的行是:vFile = .SelectedItems.Item(.SelectedItems .Count)
  • 在@simpLE MAn(在A块中)提供的代码中,将vFile = .SelectedItems.Item(.SelectedItems.Count)行替换为If .SelectedItems.Count &gt; 0 Then vFile = .SelectedItems.Item(.SelectedItems.Count)这一行
  • @tombannister; If CBool(.Show) Then 行应该捕获该错误(当您单击“取消”时。请查看编辑。
【解决方案2】:

另一种选择:

替换:

vFile = Application.GetSaveAsFilename _
(InitialFileName:=sFile, _
    FileFilter:="PDF Files (*.pdf), *.pdf", _
    Title:="Select Folder and FileName to save")

If vFile <> "False" Then

作者:

With Excel.Application.FileDialog(msoFileDialogSaveAs)

    Dim i As Integer
    For i = 1 To .Filters.Count
        If InStr(.Filters(i).Extensions, "pdf") <> 0 Then Exit For
    Next i

    .FilterIndex = i
    .InitialFileName = sFile
    .Title = "Select Folder and FileName to save"

    '------------------- Bloc A -------------------------
    If CBool(.Show) Then
        vFile = .SelectedItems.Item(.SelectedItems.Count)
    End If

    If vFile <> "" Then
    '------------------- Bloc A -------------------------

    '----------- Or replace "Bloc A" by------------------
    'If Not CBool(.Show) Then Exit Sub
    'vFile = .SelectedItems.Item(.SelectedItems.Count)

    'And remove the "If vFile <> "False" Then" check
    '----------------------------------------------------

End With

如果您选择了现有文件,则会显示覆盖消息

【讨论】:

  • 这个方法有效,它做我想做的事,但现在如果我取消保存代码错误
  • 这种方式很好,但 paul bica 提供了另一个答案,该答案获得了您通常会看到的通常的另存为对话框。感谢您的帮助
  • @tombannister;很抱歉,但我的代码显示了通常由窗口显示的确切弹出窗口。我认为您混淆了答案,因为您在 Paul Bica 的 cmets 中询问了我的回答中的台词。但如果他的答案最适合你,我没关系。感谢您的支持。
  • 绝妙的答案!我喜欢我可以使用 Excel 的“覆盖”机制,而不是自己发明。
  • 这是一个很好的答案。此方法的唯一缺点是您不能将文件过滤器限制为特定的文件类型(即 pdf)。有一些不优雅的解决方法(即之后更改文件扩展名)。尽管办公室里有什么帮助,但您无法清除此类对话的过滤器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
相关资源
最近更新 更多