Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba ActiveWorkbook.SaveAS文件名:=使用特殊字符_Vba_Excel - Fatal编程技术网

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

Vba ActiveWorkbook.SaveAS文件名:=使用特殊字符,vba,excel,Vba,Excel,诚然,我不太懂行话,所以尽管我认为我已经对此进行了彻底的研究,但可能在某个地方有完美的答案。这是我的困境,我正在开发这个Excel VBA宏来备份和恢复工作表(基本上给我无限的撤销到我指定的点,并缩短保存和重新打开的时间): 正常情况下,它可以正常工作,但就在昨天,它开始不工作,在玩了它之后,我意识到这是因为我在一个文件名中有Ω符号的文件上尝试它 基本过程是在当前目录中查找活动工作簿的文件名,但在末尾加上(备份)。它要么创建一个,要么用找到的替换打开的。但是,当在Ω文件上执行此操作时,它会将该字

诚然,我不太懂行话,所以尽管我认为我已经对此进行了彻底的研究,但可能在某个地方有完美的答案。这是我的困境,我正在开发这个Excel VBA宏来备份和恢复工作表(基本上给我无限的撤销到我指定的点,并缩短保存和重新打开的时间):

正常情况下,它可以正常工作,但就在昨天,它开始不工作,在玩了它之后,我意识到这是因为我在一个文件名中有Ω符号的文件上尝试它

基本过程是在当前目录中查找活动工作簿的文件名,但在末尾加上(备份)。它要么创建一个,要么用找到的替换打开的。但是,当在Ω文件上执行此操作时,它会将该字符替换为O。再次运行时,它显然会正确搜索Ω,因为它找不到任何字符(即使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,但那是关键。它不仅能够搜索并识别带有特殊字符的文件,而且似乎还可以删除它。将其融入代码,使其无论是否使用特殊字符都能完美运行:

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

注意:我重新编写了构建新文件名的部分,因为我认为这可能是问题的根源,但是我现在不太确定。嗨,Rachel,谢谢你花时间来处理这个问题!我使用名为“Bαckup TεsterΩ.xlsm”的文件对您的代码进行测试。您的代码能够确认该文件存在,这比我的更进一步。它在Kill命令处出错,调试器将变量显示为“Backup Tester O.xlsm”。是否有其他方法可以通过Kill来删除文件?我更新了代码,使用
fs.delete
命令而不是
Kill
命令
Kill
可能与
Dir
有相同的问题,所以这应该适合您。谢谢Rachel!这将是伟大的!我也很喜欢你建立新文件名的方式;它是如此简单和高效!如果我不是这样一个新成员,我会向你推荐。
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