VBA SaveAs方法不保存文件,没有错误?

在任何人quickflags之前:不,我没有忘记实际保存GetSaveAsFilename后的文件。

基本上,我有一个大的VBA模块,它以Excel文件开始,处理一堆数据,并在Excel中生成摘要。 我希望它是不可能覆盖的文件,我需要它在所有情况下工作(networking驱动器,从电子邮件打开等)。 这就是为什么我认为最好打开一个SaveAs框 – 离开用户path的责任。 但是,当我通过保存启用macros来触发这个方法的时候,除了文件本身不保存以外,所有的行为都像预期的那样。 debugging器说,在调用SaveAs方法的时候,fileName是它应该是的,所以我真的被困在这里。 没有错误抛出。

感谢任何能提供帮助的人! 我的代码如下:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim fileName As String, oldName As String, fullName As String Dim fragName() As String, noExtension As String, filePath As String Dim newName As String Cancel = True oldName = ThisWorkbook.Name fullName = ThisWorkbook.fullName fragName() = Split(fullName, ".", 2) noExtension = fragName(0) filePath = ThisWorkbook.Path & "\" Application.enableEvents = False enterName: fileName = Application.GetSaveAsFilename(InitialFileName:=filePath, _ FileFilter:="Microsoft Excel Worksheet (*.xlsx), *.xlsx") On Error GoTo getOut If fullName = fileName Then MsgBox ("You have chosen the same name, " & oldName & vbCr _ & ", please choose something different.") GoTo enterName ElseIf fileName = "False" Then GoTo getOut End If ThisWorkbook.SaveAs (fileName) getOut: Application.enableEvents = True End Sub 

感谢凯尔对我原来的问题的评论,我发现解决scheme是要改变的

 fileName = Application.GetSaveAsFilename(InitialFileName:=filePath, _ FileFilter:="Microsoft Excel Worksheet (*.xlsx), *.xlsx") 

至:

 fileName = Application.GetSaveAsFilename(InitialFileName:=filePath, _ FileFilter:="Microsoft Excel Macro-Enabled Worksheet (*.xlsm), *.xlsm") 

理想情况下,我可以摆脱macros,但这回答了这个问题。