ActiveWorkbook.SaveAS文件名:=使用特殊字符

无可否认,我对行话的理解不好,所以当我我彻底研究过这个问题的时候,在某个地方可能会有完美的答案。 这是我的困境,我正在开发这个Excel VBAmacros来备份和恢复工作表(基本上给我无限的Undos点,我指定的和保存和重新打开的捷径):

Public BULast As String Sub Backup() 'This macro imitates videogame save-states. It will save a backup that can replace to current workbook later if you've made an irreversible mistake. 'Step 1: Agree to run away if things go wrong (establish an error handler) On Error GoTo BackupError 'Step 2: Create some variables Dim OriginalFile As String Dim BUDir As String Dim BUXAr() As String Dim BUExt As String Dim BUNam As String Dim BackupFile As String 'Step 3: Define those variables OriginalFile = ActiveWorkbook.FullName BUDir = ActiveWorkbook.Path BUXAr = Split(ActiveWorkbook.FullName, ".") BUExt = BUXAr(UBound(BUXAr)) BUNam = Replace(ActiveWorkbook.Name, "." & BUExt, "") & " (Back-Up)" BackupFile = BUDir & "\" & BUNam & "." & BUExt 'Step 4: Hide the truth Application.ScreenUpdating = False Application.DisplayAlerts = False 'Step 5(A): If there is no backup file, create one using the same file name as the one you're working with and throw a " (Back-up)" on it. If Dir(BackupFile) = "" Then ActiveWorkbook.SaveAs filename:=BackupFile ActiveWorkbook.Close Workbooks.Open filename:=OriginalFile BUYoN = vbNo BULast = Date & ", " & Time MsgBox "A Backup has been created!" Else BUYoN = MsgBox("This will restore the " & BULast & " backup and undo all changes made to this project. Continue?" _ , vbYesNo, "Revert to Backup?") End If 'Step 5(B): If a backup has been created, restore it over the current workbook and delete the backup. If BUYoN = vbYes Then ActiveWorkbook.Close Workbooks.Open filename:=BackupFile ActiveWorkbook.SaveAs filename:=OriginalFile Kill (BackupFile) BUCheck = "Dead" End If 'Step 6: Put things back to the way you found them, you're done! Application.ScreenUpdating = True Application.DisplayAlerts = True Exit Sub 'Step 1 (Continued): If nothing went wrong, stop worrying about it, if something did, say it didn't work and go away. On Error GoTo 0 BackupError: MsgBox "Attempt to Backup or Restore was unsuccessful" End Sub 

通常情况下,它按预期工作,但就在昨天它开始不工作,玩了之后,我意识到这是因为我试图在一个文件名中有一个Ω符号的文件。

基本过程是在当前目录中查找活动工作簿的文件名,但在最后使用(备份)。 它会创build一个,或者用它find的replace它。 然而,当在一个Ω文件上完成时,它会用一个Oreplace该字符。当再次运行时,它显然正确地search Ω,因为它找不到任何(即使在那里明显的O替代文件)。

我知道最简单的解决办法就是确保人们把他们的文件名保持在键盘上可以看到的状态,但这对我不起作用。 我几乎笃信把代码中的适应性而不是用户。 那么这个啰嗦的背后故事,这里是我的具体问题:

VBA中是否有SaveAs函数或实用的解决方法,可以处理指定文件名中的特殊字符?

问题在于Dir()函数,因为它在检查文件之前将特殊字符转换为ANSI,因此在这些情况下失败。 改用FileSystemObject对象:

 Sub Backup() On Error GoTo BackupError Dim OriginalFile As String OriginalFile = ActiveWorkbook.FullName ' get back up file name Dim BackupFile As String Dim pos As Integer pos = InStrRev(OriginalFile, ".") BackupFile = Mid$(OriginalFile, 1, pos - 1) & " (Back-Up)." & Mid$(OriginalFile, pos + 1) Application.ScreenUpdating = False Application.DisplayAlerts = False 'Step 5(A): If there is no backup file, create one using the same file name as the one you're working with and throw a " (Back-up)" on it. Dim BUYoN As VbMsgBoxResult Dim BULast As String Dim fs As Object Set fs = CreateObject("Scripting.FileSystemObject") With fs If Not .FileExists(BackupFile) Then ActiveWorkbook.SaveAs Filename:=BackupFile ActiveWorkbook.Close Workbooks.Open Filename:=OriginalFile BUYoN = vbNo BULast = Date & ", " & Time MsgBox "A Backup has been created!" Else BUYoN = MsgBox("This will restore the " & BULast & " backup and undo all changes made to this project. Continue?" _ , vbYesNo, "Revert to Backup?") End If End With 'Step 5(B): If a backup has been created, restore it over the current workbook and delete the backup. If BUYoN = vbYes Then ActiveWorkbook.Close Workbooks.Open Filename:=BackupFile ActiveWorkbook.SaveAs Filename:=OriginalFile 'Kill (BackupFile) fs.Delete BackupFile Dim BUCheck As String BUCheck = "Dead" End If 'Step 6: Put things back to the way you found them, you're done! Application.ScreenUpdating = True Application.DisplayAlerts = True Exit Sub On Error GoTo 0 BackupError: MsgBox "Attempt to Backup or Restore was unsuccessful" End Sub 

我知道我们不应该提供意见,但我认为瑞秋是一个天才! 我不知道FileSystemObject,但最终成为关键。 它不仅能够search和识别带有特殊字符的文件,而且它似乎也可以删除它。 将它合并到代码中可使其运行得非常完美,无论是否带有特殊字符:

 Public BULast As String Sub Backup() 'This macro imitates videogame save-states. It will save a backup that can replace the 'current workbook later if you've made an irreversible mistake. 'Step 1: Agree to run away if things go wrong (establish an error handler) On Error GoTo BackupError 'Step 2: Create some variables Dim OriginalFile As String Dim BUDir As String Dim BUXAr() As String Dim BUExt As String Dim BUNam As String Dim BackupFile As String Dim BUfs As Object 'Step 3: Define those variables OriginalFile = ActiveWorkbook.FullName BUDir = ActiveWorkbook.Path BUXAr = Split(ActiveWorkbook.FullName, ".") BUExt = BUXAr(UBound(BUXAr)) BUNam = Replace(ActiveWorkbook.Name, "." & BUExt, "") & " (Back-Up)" BackupFile = BUDir & "\" & BUNam & "." & BUExt Set BUfs = CreateObject("Scripting.FileSystemObject") 'Step 4: Hide the truth Application.ScreenUpdating = False Application.DisplayAlerts = False 'Step 5(A): If there is no backup file, create one using the same file name as the one 'you're working with and throw a " (Back-up)" on it. With BUfs If Not .FileExists(BackupFile) Then ActiveWorkbook.Save ActiveWorkbook.SaveAs filename:=BackupFile ActiveWorkbook.Close Workbooks.Open filename:=OriginalFile BUYoN = vbNo BULast = Date & ", " & Time MsgBox "A Backup has been created!" Else BUYoN = MsgBox("This will restore the " & BULast & " backup and undo all changes made to this project. Continue?" _ , vbYesNo, "Revert to Backup?") End If End With 'Step 5(B): If a backup has been created, restore it over the current workbook and 'delete the backup. If BUYoN = vbYes Then ActiveWorkbook.Close Workbooks.Open filename:=BackupFile ActiveWorkbook.SaveAs filename:=OriginalFile BUfs.DeleteFile BackupFile, True End If 'Step 6: Put things back to the way you found them, you're done! Application.ScreenUpdating = True Application.DisplayAlerts = True Exit Sub 'Step 1 (Continued): If nothing went wrong, stop worrying about it, if something did, 'say it didn't work and go away. On Error GoTo 0 BackupError: MsgBox "Attempt to Backup or Restore was unsuccessful" End Sub