禁止“另存为”提示

时间:2017-02-11 22:47:37

标签: excel vba excel-vba csv prompt

我查看了这个主题并找到了一些帮助,但这些建议似乎没有起作用。

  • 我打开一个CSV文件到EXCEL进行一些更改,然后想要将结果保存回相同的文件名和CSV格式。
  • 我想在没有提示确保我要保存文件的情况下这样做。
  • 我们正在使用启用宏的Excel文件导入数据生成更改然后保存。
  • 整个过程由批处理文件启动,该文件将定期打开Excel应用程序和指定文件,这就是为什么我们不希望提示停止该过程。

这是我在VBA中用来完成工作的代码,以及我发现的其他可以帮助我抑制提示的子代码。

此代码位于文件的TheWorkbook,而不是模块。

我错过了什么吗?

Sub fixfile()
    Const strFileName = "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv"
    Dim wbkS As Workbook
    Dim wshS As Worksheet
    Dim wshT As Worksheet
    Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    Set wbkS = Workbooks.Open(Filename:=strFileName)
    Set wshS = wbkS.Worksheets(1)
    wshS.UsedRange.Copy Destination:=wshT.Range("A1")
    wbkS.Close SaveChanges:=False

    'This is the area of work that we doing to the data
    'Through here

    Application.DisplayAlerts = False 'IT WORKS TO DISABLE ALERT PROMPT

    ActiveWorkbook.SaveAs Filename:= _
        "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv", FileFormat _
        :=xlCSVMSDOS, CreateBackup:=False

    Application.DisplayAlerts = True 'RESETS DISPLAY ALERTS
    Application.Quit
End Sub

Private Sub Workbook_Open()
    fixfile
End Sub

Sub CloseandSave()
    ActiveWorkbook.Close SaveChanges:=True
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ThisWorkbook.Save
End Sub

5 个答案:

答案 0 :(得分:11)

您的代码中存在以下问题。

当您在启用宏的工作簿上调用SaveAs时,您已经在其中附加了一个工作表:

  

Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))

然后您尝试将其保存为csv,这是一个只包含一个工作表的文本文件,因此Excel会抱怨您将丢失信息。

此外,您要对csv进行两次更新:一次在

  

ActiveWorkbook.SaveAs Filename:= ...

因此,当前工作簿将成为已保存的工作簿,然后再次显示在Workbook_BeforeClose中。在后者中你不会禁用警报,但无论如何都不需要再次保存。

我得出结论,你想要的是使用支持宏的wb作为计算工具,而你只关心更新CSV工作簿。

为简单起见,我们将禁用整个会话的警报,因为启用宏的WB用作实用程序,我们不希望批处理作业因任何原因而停止。但是,如果您对此感觉更舒服,可以在保存之前和之后以传统方式进行。

' Code module ThisWorkbook
Option Explicit
Private Sub Workbook_Open()
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    fixCSVFile

    ' Do the following only if you want the macro-enabled WB to keep
    ' a copy of the CSV worksheet. but my feeling is you dont want to
    ' ThisWorkbook.Save
    '''''''''''''''''''''

     Application.Quit
End Sub

Sub fixCSVFile()
    Const strFileName = "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv"
    Dim wbkS As Workbook, wshS As Worksheet, wshT As Worksheet

    Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    Set wbkS = Workbooks.Open(Filename:=strFileName)
    Set wshS = wbkS.Worksheets(1)

    wshS.UsedRange.Copy Destination:=wshT.Range("A1")
    wbkS.Close SaveChanges:=False

    'This is the area of work that we doing to the data
    ' For purpose of testing:
    wshT.Range("A1").Value = wshT.Range("A1").Value + 1

    ' Now we will export back the modified csv
    wshT.Move '<- Here we have a temporary workbook copy of the modified csv
    With ActiveWorkbook
        .SaveAs Filename:=strFileName, FileFormat:=xlCSVMSDOS, CreateBackup:=False
        .Close False
    End With
End Sub

还有一件事,启用宏的WB现在会在打开时立即关闭,因此很难编辑或修改(尽管有解决方法)。因此,您应该在没有Application.Quit的情况下保存它的备份副本,作为测试/维护副本。只有为批处理作业而放入生产中的副本才应具有Application.Quit语句。

答案 1 :(得分:6)

根据答案中的评论,打开文件的原因并立即保存,没有其他更改......

  

所以我们需要做我们正在做的事情来获取文件编辑日期   改变但不是实际的文件。

...这是 完整 X-Y problem。如果您需要更改文件的修改时间,只需更改文件的修改时间,而不是跳过所有打开和重新保存的箍:

var newName = sheetName.Replace('$', '');

这将比您当前的流程快得多,只会将文件修改日期和时间设置为您运行Sub UpdateFileModifiedDate() Const filePath = "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv" Dim handle As Integer handle = FreeFile Open filePath For Binary As #handle 'Read the first byte. Dim first As Byte Get #handle, 1, first 'Write it back Put #handle, 1, first Close #handle End Sub 的时间,并且不会冒任何其他可以运行的问题的风险通过Excel循环CSV文件(日期格式和区域设置问题,截断小数,转换为指数表示法等等)。

答案 2 :(得分:5)

因为你有意识地 覆盖现有文件,你可以这样做:

  • 首先使用Kill命令

  • 删除它
  • 然后执行SaveAs

所以更改此代码部分:

'This is the area of work that we doing to the data
'Through here

Application.DisplayAlerts = False 'IT WORKS TO DISABLE ALERT PROMPT

ActiveWorkbook.SaveAs Filename:= _
    "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv", FileFormat _
    :=xlCSVMSDOS, CreateBackup:=False

Application.DisplayAlerts = True 'RESETS DISPLAY ALERTS
Application.Quit

到此:

'This is the area of work that we doing to the data
'Through here

Kill strFileName '<-- delete the old file

ActiveWorkbook.SaveAs Filename:= _
    "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv", FileFormat _
    :=xlCSVMSDOS, CreateBackup:=False
Application.Quit

此外,您可以通过正确处理ActiveWorkbookActiveSheet对象并减少变量和代码量来重构您的代码,如下所示:

Sub fixfile()
    Const strFileName = "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv"

    Workbooks.Open(Filename:=strFileName).Worksheets(1).UsedRange.Copy Destination:=Worksheets.Add(After:=Worksheets(Worksheets.Count)).Range("A1") '<--| open 'strFileName', reference and copy its 1st worksheet 'UsedRange' and paste it to a newly added worksheet in the macro workbook. After this statement we're left with the opened workbook as `ActiveWorkbook`
    ActiveWorkbook.Close SaveChanges:=False '<--| close `ActiveWorkbook`, i.e. the just opened one. We're left with macro workbook as `ActiveWorkbook` and its newly created worksheet as `ActiveSheet`

    'This is the area of work that we doing to the data
    'Through here

    ActiveSheet.Move '<--| move `ActiveSheet` (i.e. the newly created sheet in macro workbook) to a "new" workbook having that sheet as its only one. We're left with this "new" workbook as `ActiveWorkbook`
    Kill strFileName '<--| delete the "old" 'strFileName'
    ActiveWorkbook.SaveAs Filename:=strFileName, FileFormat:=xlCSVMSDOS, CreateBackup:=False '<--| save `ActiveWorkbook` (i.e the "new" one) as the new 'strFileName' file
    ActiveWorkbook.Close SaveChanges:=False '<--| close `ActiveWorkbook` (i.e the "new" one) without changes (we just "SavedA"s it)
    Application.Quit     
End Sub

答案 3 :(得分:3)

您似乎正在对两个文件进行更改。除了要打开的csv文件之外,您似乎正在使用以下行向运行VBA代码的excel文件添加工作表:

Dim wshT As Worksheet
Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))

所以我的猜测是你确实压制了csv文件的保存提示,但是当你试图关闭它时,你也得到了对excel工作簿所做更改的保存提示。所以我认为您还需要禁用该提示,同时关闭CloseAndSave子中的DisplayAlerts,或者实际关闭excel工作簿的任何地方。

答案 4 :(得分:2)

  1. 我不明白为什么要将CSV工作表复制到启用宏的工作簿中的新工作表中。 这是您的问题开始的地方!

    • 您应该只是处理wshS中的数据并保存wbkS 完成,没有更多问题。
  2. 致电时

    ActiveWorkbook.SaveAs Filename:= _
        "W:\Webshare\Documents Acquired in 2017\Jim Excel\snr-room-schedule.csv", 
        FileFormat:=xlCSVMSDOS, CreateBackup:=False`
    

    您在Excel中将当前启用宏的文件重命名为CSV文件,只要Excel看到它

  3. 调用Application.Quit时,它将调用

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        ThisWorkbook.Save
    End Sub
    

    您正在抱怨的提示正在发生。

    • 即使您将其删除,在调用Workbook_BeforeClose后, Excel 仍会检查所有打开的文件“.Saved标记。
    • Excel 会提示您保存.Saved = False
    • 所有文件
  4. 但如果您设置 ThisWorkbook.Saved = True ,则 Excel 将关闭该文件而不要求保存。

    <强>解决方案:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        ThisWorkbook.Saved = True
    End Sub