0

当我使用 shell 命令行来使用 winzip 时,在 vba 中使用命令行时,返回路径和文件名的变量 r 和 q 不会被识别为字符串。我还需要什么代码来跳过目录中已经存在的任何 zip 文件。

Option Explicit
Public Function ZipAll()
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim colFiles As Scripting.File
Dim objFile As Object
Dim objStartFolder As String
Dim Subfolder As Scripting.Folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "S:\UPSData\EOMOnHand\Abbott\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
ShowSubFolders objFSO.GetFolder(objStartFolder)
End Function

Public Function ShowSubFolders(Folder)
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim colFiles As Scripting.File
Dim objFile As Object
Dim objStartFolder As String
Dim Subfolder As Scripting.Folder
Dim r As String
Dim q As String
Dim NextRow As Long
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    NextRow = 1

    For Each Subfolder In Folder.Subfolders
    'MsgBox SubFolder.Path
    Set objFolder = objFSO.GetFolder(Subfolder.Path)
    Set colFiles = objFolder.Files
    For Each objFile In colFiles
    r = Subfolder.Path & "\" & objFile.Name & ".zip"
    q = Subfolder.Path & "\" & objFile.Name
    MsgBox r
    MsgBox q
    Shell "C:\Program Files\WinZip\WinZip64.exe -min -a " & r & " " & q
    NextRow = NextRow + 1
    Next
Next
End Function

当我 msgbox qi 返回 s:\upsdata\eomonhands\abbott\abbott.xlsx。我在调用 winzip 的命令行中使用 thata 作为文件名,但它不会将其视为字符串。如何将 q 作为字符串返回。还有什么代码可以过滤掉该文件夹中已经是 zip 文件的任何其他文件。我不想压缩那些。

4

1 回答 1

0

要压缩 xlsx 子文件夹中的所有文件,您可以执行以下操作:

Public Function ZipAll()
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim colFiles As Scripting.Files
Dim objStartFolder As String
Dim Subfolder As Scripting.Folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "S:\UPSData\EOMOnHand\Abbott\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
ShowSubFolders objFSO.GetFolder(objStartFolder)
End Function

Public Function ShowSubFolders(Folder)
Dim objFSO As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder
Dim colFiles As Scripting.Files
Dim objFile As Object
Dim objStartFolder As String
Dim Subfolder As Scripting.Folder
Dim FileName As String
Dim r As String
Dim q As String
Dim NextRow As Long
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    NextRow = 1

    For Each Subfolder In Folder.Subfolders
    Set objFolder = objFSO.GetFolder(Subfolder.Path)
    Set colFiles = objFolder.Files
    For Each objFile In colFiles
    FileName = Subfolder.Path & "\" & objFile.Name
    If Right(FileName, 5) = ".xlsx" Then
    GoTo Line1
    Else
    GoTo Line2
    End If

Line1:
    r = Chr(34) & Subfolder.Path & "\" & objFile.Name & ".zip" & Chr(34)
    q = Chr(34) & Subfolder.Path & "\" & "*.xlsx" & Chr(34)
    iRunTaskID = Shell("C:\Program Files\WinZip\WinZip64.exe -min -a " & r & " " & q)
    DoEvents
    hProc = OpenProcess(SYNCHRONIZE, 0, iRunTaskID)
    If hProc <> 0 Then
    WaitForSingleObject hProc, INFINITE
    CloseHandle hProc
End If
    r = Subfolder.Path & "\" & objFile.Name & ".zip"
    If Len(Dir$(r)) > 0 Then
    GoTo Line3
    Else
    GoTo Line2
    End If
Line3:
bfile = Subfolder.Path & "\" & objFile.Name
If Len(Dir$(bfile)) > 0 Then
 Kill bfile
End If
Line2:
    NextRow = NextRow + 1
    Next
Next

End Function

本质上,如果您将过滤器放在文件名上,它将跳转到压缩文件并压缩该文件的行,而不是跳过列出的其他文件。如果您在 winzp 命令行中使用 chr(34),则在调用时允许它是一个字符串。

于 2017-08-08T22:43:57.573 回答