vba - workaround for issue excel saving temp files

后端 未结 2 1834
情书的邮戳
情书的邮戳 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
    

提交回复
热议问题