vba - 问题excel保存临时文件的解决方法

时间:2016-05-06 10:15:04

标签: excel vba excel-vba

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

下面的代码执行'save-as',基于文件名以值结尾(例如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

2 个答案:

答案 0 :(得分:0)

我在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

答案 1 :(得分:0)

感谢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

更新了罗宾的代码:

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