VBA SaveAs不保存 - 没有错误 [英] VBA SaveAs Doesn't Save - No Error

查看:95
本文介绍了VBA SaveAs不保存 - 没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我这里几年不需要问一个问题 - 一般通过搜索各种论坛找到我需要的东西。然而,这个让我难过。我在网上找到了许多类似标题的问题和解决方案,但没有解决我的问题。下面代码运行的
代码按预期运行,因为我可以单步调试代码,并在vFile中使用合法字符串获取.SaveAs命令,但文件根本不会保存。

I haven't needed to ask a question here for a few years - generally finding what I need by searching the various forums. However, this one has me stumped. I found numerous similarly titled questions and solutions on the web but none solve my problem. The following code runs as expected, in that I can step through the code and it gets to the .SaveAs command with a legitimate string in vFile but the file simply isn't saved.

'In the ThisWorkbook module

'在标准模块中

Public Sub ThisWorkbook_BeforeSave(ByVal SaveAsUI As Boolean ,取消为布尔值)
Dim vFile As Variant

如果ThisWorkbook.Path =""那么
如果不是AIPlist则没有那么
AIPlist.SaveIdList

Application.EnableEvents = False
vFile = Application.GetSaveAsFilename(InitialFileName:= GetFileName&" ;. xlsb",_
FileFilter:=" Excel二进制文件(* .xlsb),*。xlsb,所有文件(*。*),*。*")

如果是vFile <> False然后ThisWorkbook.SaveAs文件名:= vFile,FileFormat:= 50

Application.EnableEvents = True
取消= True
否则
MsgBox("无法保存但是。)
取消=真
结束如果
否则
'让Excel做它的事情
取消=假
结束如果
End Sub

Public Sub ThisWorkbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim vFile As Variant If ThisWorkbook.Path = "" Then If Not AIPlist Is Nothing Then AIPlist.SaveIdList Application.EnableEvents = False vFile = Application.GetSaveAsFilename(InitialFileName:=GetFileName & ".xlsb", _ FileFilter:="Excel Binary files (*.xlsb), *.xlsb, All files (*.*), *.*") If vFile <> False Then ThisWorkbook.SaveAs Filename:=vFile, FileFormat:=50 Application.EnableEvents = True Cancel = True Else MsgBox ("Nothing to save yet.") Cancel = True End If Else ' Let Excel do it's thing Cancel = False End If End Sub

如果它有任何区别,我应该指出文件本身是.xltm模板的产品。

In case it makes any difference, I should point out that the file itself is the product of a .xltm template.

如果有人想要问,我总是将ThisWorkbook,Worksheet和Form模块中的代码保持在绝对最小值,因为它们无法"清理"。因此,调用标准模块。

And in case anyone's tempted to ask, i always keep code in the ThisWorkbook, Worksheet and Form modules to an absolute minimum as they can't be 'cleaned'. Hence, the call to a standard module.

推荐答案


以下代码按预期运行,因为我可以单步执行代码,并使用vFile中的合法字符串访问.SaveAs命令,但文件不会保存。

The following code runs as expected, in that I can step through the code and it gets to the .SaveAs command with a legitimate string in vFile but the file simply isn't saved.

如果它有任何区别,我应该指出文件本身是.xltm模板的产品。

In case it makes any difference, I should point out that the file itself is the product of a .xltm template.

由于未知变量,我复制了代码并且必须对其进行修改。

I've copied the code and have to modified it, because of unknown variables.

保存了一个XLTM文件。 Excel关闭,双击XLTM,CTRL-S并输入"test",单击"保存"并保存文件。没问题。

Saved a XLTM file. Excel closed, double-click the XLTM, CTRL-S and enter "test", click Save and the file is saved. No issue.

Andreas。

Public Sub ThisWorkbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  Dim vFile As Variant
  Dim GetFileName

  If ThisWorkbook.Path = "" Then
    If True Then
'    If Not AIPlist Is Nothing Then
'      AIPlist.SaveIdList

      Application.EnableEvents = False
      vFile = Application.GetSaveAsFilename(InitialFileName:=GetFileName & ".xlsb", _
        FileFilter:="Excel Binary files (*.xlsb), *.xlsb, All files (*.*), *.*")

      If vFile <> False Then ThisWorkbook.SaveAs Filename:=vFile, FileFormat:=50

      Application.EnableEvents = True
      Cancel = True
    Else
      MsgBox ("Nothing to save yet.")
      Cancel = True
    End If
  Else
    ' Let Excel do it's thing
    Cancel = False
  End If
End Sub


这篇关于VBA SaveAs不保存 - 没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆