【发布时间】:2017-08-06 21:24:23
【问题描述】:
当我使用 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 q 我返回 s:\upsdata\eomonhands\abbott\abbott.xlsx。我在调用 winzip 的命令行中使用 thata 作为文件名,但它不会将其视为字符串。如何将 q 作为字符串返回。还有什么代码可以过滤掉该文件夹中已经是 zip 文件的任何其他文件。我不想压缩那些。
【问题讨论】:
-
您肯定在该命令中得到了语法错误 -
C:\Program Files\WinZip\WinZip64.exe -min -as:\UPSData\EOMOnHand\Abbott\xyz\abc.xlsxs:\UPSData\EOMOnHand\Abbott\xyz\*.xlsx将无效,因为-as:\UPSData\EOMOnHand\Abbott\xyz\abc.xlsxs:\UPSData\EOMOnHand\Abbott\xyz\*.xlsx不是一个合理的参数(但我没有安装 WinZip 来检查正确的语法是) -
@YowE3K 好的,忘记 winzip 部分。当我到达 objfile.Name 时,它说需要对象。我重新编写了代码以包含 subolders 路径。它不会显示名称。
-
“当它到达 ojbFile.Name 行时,它说 Object Require”,真的吗?
For Each objFile in colFiles应该保证它被填充了! -
r和q定义在哪里?请在代码顶部添加Option Explicit以强制声明变量吗? -
代码(WinZip 问题除外)对我有用。