2015-05-20 153 views
1

無可否認,我對行話不熟悉,所以雖然我認爲我對此進行了深入研究,可能有某種完美的答案。這裏是我的困境,我開發這個Excel VBA宏備份和恢復的工作表(基本上給了我無限索馬里發展事務處的時候,我指定和短切圍繞節能和重新打開):ActiveWorkbook.SaveAS文件名:=使用特殊字符

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 

常於預計,但就在昨天它開始無法正常工作,並在玩了它之後,我意識到這是因爲我在文件名中帶有Ω符號的文件上嘗試它。

的基本過程是在活動工作簿的文件名,在當前目錄查找,但(備份)上漲結尾。它會創建一個,或者用它找到的替換它。當然而,一個Ω文件來完成,它替換用O.這個角色的時候再次運行,它顯然搜索的的Ω正確的,因爲它無法找到任何(甚至在衆目睽睽那裏O型替代文件) 。

我知道最簡單的解決辦法是,只要確保人們保持它們的文件名是什麼,你可以在鍵盤上看到的,但是,這並不爲我工作;我幾乎是宗教關於把適應性的代碼而不是用戶。所以對於這個冗長的背景故事,這裏是我的具體問題:

VBA中是否有SaveAs函數或實用的解決方法,可以處理指定文件名中的特殊字符?

回答

0

我知道我們不應該提供的意見,但我認爲,Rachel是個天才!我不知道FileSystemObject,但最終成爲關鍵。它不僅能夠搜索和識別帶有特殊字符的文件,而且它似乎也可以刪除它。合併了入代碼使得帶或不帶特殊字符的完美運行:

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 
1

的問題就出在迪爾()函數,它檢查的文件之前,特殊字符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 
+0

注:我重新寫,因爲我認爲這可能是問題的來源,建立新的文件名的部分,但是我現在不太確定。 –

+0

嗨Rachel,並感謝您花時間在此工作!我使用名爲「BαckupTεsterΩ.xlsm」的文件將代碼放入測試中。你的代碼能夠確認文件存在,這比我的更進一步。它在Kill命令中出現錯誤,調試器將該變量顯示爲「Backup Tester O.xlsm」。是否有替代殺死刪除文件? –

+0

我更新了代碼以使用'fs.Delete'命令而不是'Kill'​​命令。 'Kill'​​可能與'Dir'有相同的問題,所以這應該適合你。 –