vba - workaround for issue excel saving temp files

后端 未结 2 1833
情书的邮戳
情书的邮戳 2021-01-23 02:26

When saving a specific workbook, Excel creates a temp file instead of saving the data (without displaying an error or warning message). The symptoms are roughly the same as desc

相关标签:
2条回答
  • 2021-01-23 02:49

    Thanks to Robin the working solution is as follows:

    Updated intial code:

        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
    

    Updated Robin's code:

    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
    
    0 讨论(0)
  • 2021-01-23 03:06

    I tested this Sub in Excel 2010 and it works for me. I immediately break the loop after deleting the file as I assume the indexing may get out of alignment with the loop. A more refined variant might loop through the recent file list and create a collection of indices to delete, then iterate backward over that collection and delete each entry in turn.

    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
    
    0 讨论(0)
提交回复
热议问题