在 Vba Excel 中提取 .rar 文件

Extract .rar file in Vba Excel

我在 Excel 中有一个 vba 宏,它从共享点站点下载 .rar 文件并将其解压缩。下载工作正常,我可以在文件夹中看到下载的文件,但无法提取文件。 我将只粘贴提取文件的部分,因此我的代码如下所示

Sub Extract()

    Dim RarIt As String
    Dim Source As String
    Dim Desti As String
    Dim WinRarPath As String

    WinRarPath = "C:\Program Files\WinRar\"
    Source = "C:\Reports\EMEA Load.rar"
    Desti = "C:\Reports\"

    RarIt = Shell(WinRarPath & "WinRar.exe e " & Source & " " & Desti, vbNormalFocus)

End Sub

所以当我 运行 它时,它 returns 一个消息框 "no archives found" 虽然我可以在文件夹中看到 .rar 文件。

我创建了另一个解决方案,一个解压缩文件的功能

Function Unzip(str_FILENAME As String)

    Dim oApp As Object
    Dim Fname As Variant
    Dim FnameTrunc As Variant
    Dim FnameLength As Long
    Dim FnamePath As Long

    Fname = str_FILENAME
    FnamePath = InStrRev(Fname, "\")
    FnameTrunc = Left(Fname, FnamePath)

    Set oApp = CreateObject("Shell.Application")
    oApp.Namespace(FnameTrunc).CopyHere oApp.Namespace(Fname).Items

End Function

我在我的 Sub 中调用这个函数,它对 .zip 文件工作正常但对 .rar 文件它 returns 这个错误

"method of Namespace object ishelldispatch6 failed"

Sub Extract2()

    Dim strFilePath As String
    strFilePath = "C:\Reports\EMEA Load.rar"

     Unzip (strFilePath)

End Sub

请问我需要更改我的代码有什么建议吗?非常感谢。

因为文件名中有空格,所以我们需要用 Chr(34) 将其括起来:

Sub Extract()

    Dim RarIt As String
    Dim Source As String
    Dim Desti As String
    Dim WinRarPath As String

    WinRarPath = "C:\Program Files\WinRar\"
    Source = "C:\Reports\EMEA Load.rar"
    Desti = "C:\Reports\"

    RarIt = Shell(Chr(34) & WinRarPath & "WinRar.exe" & Chr(34) & " e " & Chr(34) & Source & Chr(34) & " " & Chr(34) & Desti & Chr(34), vbNormalFocus)

End Sub