【问题标题】:vba - workaround for issue excel saving temp filesvba - excel保存临时文件问题的解决方法
【发布时间】:2016-09-01 09:01:20
【问题描述】:

保存特定工作簿时,Excel 会创建一个临时文件而不是保存数据(不显示错误或警告消息)。症状与这篇文章中描述的大致相同: microsoft-excel-returns-the-error-document-not-saved-after-generating-a-2gb-temp-file 我尝试了几种解决方案,但决定实施一种变通方法,因为“另存为”工作正常。

下面的代码执行“另存为”,基于文件名以一个值结尾(例如 myFile V1.xlsm),宏将在每次保存工作簿时添加一个递增字符(a 到 z)。 (例如 myFile V1a.xlsm)。

宏在标准模块中工作正常,但在移动到“thisWorkbook”时会导致 Excel“停止响应”。我通过将它保存在标准模块中并将组合键“control-s”分配给宏来“解决”这个问题。仍然有兴趣知道它是否可以在“thisWorkbook”中使用。

此解决方法的缺点是每次增量保存都会阻塞“最近文件”列表。从最近的文件历史记录中删除以前的文件名会很好,但这似乎无法通过 VBA 完成。 (VBA - How do I remove a file from the recent documents list in excel 2007?)。有什么建议吗?

Windows 10、Excel 2016(版本 16.0.6868.2060)

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim newFilename As String
Dim oldFilename As String

oldFilename = ActiveWorkbook.Name
newFilename = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)

If IsNumeric(Right(newFilename, 1)) = True Then

    ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path + "\" + newFilename & "a.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Else
    If Right(newFilename, 1) = "z" Then
        MsgBox "'z' reached, please save as new version"
        Exit Sub
    End If

    newFilename = Left(newFilename, Len(newFilename) - 1) & Chr(Asc(Right(newFilename, 1)) + 1)
    ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path + "\" + newFilename & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End If

'potential code to remove oldFilename from 'Recent File' list

End Sub

【问题讨论】:

  • 顺便说一句,我怀疑我的 excel 文件因在工作表中复制宏框而损坏(在按下控制按钮的同时拖动框)。

标签: vba excel


【解决方案1】:

我在 Excel 2010 中测试了这个 Sub,它对我有用。我在删除文件后立即中断循环,因为我认为索引可能与循环不一致。更精细的变体可能会遍历最近的文件列表并创建要删除的索引集合,然后向后迭代该集合并依次删除每个条目。

Public Sub RemoveRecentFile(strFileName As String)

    Dim collRecentFiles As Excel.RecentFiles
    Dim objRecentFile As Excel.RecentFile
    Dim intRecentFileCount As Integer
    Dim intCounter As Integer

    Set collRecentFiles = Application.RecentFiles
    intRecentFileCount = collRecentFiles.Count

    For intCounter = 1 To intRecentFileCount
        Set objRecentFile = collRecentFiles(intCounter)
        If objRecentFile.Name = strFileName Then
            objRecentFile.Delete
            Exit For
        End If
    Next intCounter

End Sub

【讨论】:

  • 谢谢罗宾!在将 'objRecentFile.Name' 更改为 'objRecentFile.Path' 后,我让它工作,还必须更新我的代码,将 'AddToMru:=True' 属性添加到 saveAs 函数以使用新文件名更新最近的文件历史记录。将整体解决方案发布为 anwser。
【解决方案2】:

感谢 Robin,工作解决方案如下:

更新的初始代码:

    Sub incrementSaveAs()
    'to avoid that other workbooks are saved (when assigned to shortkey control-S)
    If ActiveWorkbook.Name <> ThisWorkbook.Name Then ActiveWorkbook.Save: Exit Sub

    Dim newFilename As String
    Dim oldFilename As String

        oldFilename = ActiveWorkbook.Name
        newFilename = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)

        If IsNumeric(Right(newFilename, 1)) = True Then

            ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path + "\" + newFilename & "a.xlsm", _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False, AddToMru:=True
            'AddToMru:=True Added to update recent files history

        Else
            If Right(newFilename, 1) = "z" Then
                MsgBox "'z' reached, please save as new version"
                Exit Sub
            End If

            newFilename = Left(newFilename, Len(newFilename) - 1) & Chr(Asc(Right(newFilename, 1)) + 1)
            ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path + "\" + newFilename & ".xlsm", _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False, AddToMru:=True

        End If

        RemoveRecentFile (ActiveWorkbook.Path & Application.PathSeparator & oldFilename)

    End Sub

更新了 Robin 的代码:

Public Sub RemoveRecentFile(strPathAndFileName As String)

    Dim collRecentFiles As Excel.RecentFiles
    Dim objRecentFile As Excel.RecentFile
    Dim intRecentFileCount As Integer
    Dim intCounter As Integer

    Set collRecentFiles = Application.RecentFiles
    intRecentFileCount = collRecentFiles.Count

    For intCounter = 1 To intRecentFileCount
        Set objRecentFile = collRecentFiles(intCounter)
        If objRecentFile.Path = strPathAndFileName Then
            objRecentFile.Delete
            Exit For
        End If
    Next intCounter

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多