我看过一些代码可以做到这一点,但对我来说它不起作用。我正在运行Windows 7,并且excel 2010(来自Office 2010)。我的代码:
Public Sub GetZipContents()
Dim oApp As Shell32.Shell
Set oApp = New Shell32.Shell
Dim strFile As String
Dim xFname
Dim xRow As Long
Dim newRow As Long
Dim rNew As Range
Dim fileNameInZip
Dim oFolder As Variant
Dim i As Integer
i = 1
xRow = 0
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.Title = "Select the zip to get file names from"
fd.Filters.Clear
fd.Filters.Add "Zip Files", "*.zip"
fd.FilterIndex = 1
If fd.Show = -1 Then
strFile = fd.SelectedItems(1)
oFolder = oApp.Namespace(strFile).Items
Range("A" & i).Value = strFile
i = i + 1
For Each fileNameInZip In oFolder
Range("A" & i).Value = fileNameInZip
i = i + 1
Next
Set oApp = Nothing
End If
End Sub
我还使用fileNameInZip作为Variant,但输出是相同的。无论我选择的zip文件,我的输出(附加文本版本,屏幕截图更好,但我无法附加图像,因为这是我的第一篇文章...第一行是zip文件的名称,接下来是命名空间调用中的项目总是相同的。我很茫然,因为我看到的每个网站都有类似的答案代码。任何想法发生了什么(其中的文件通常是pdf,而不是& Open等)?
C:\ Users \用户PGibson \下载\ CW985786-T-00136.zip
&安培;打开
的Cu& T公司
&安培;复制
&安培;删除
P&安培; roperties
答案 0 :(得分:2)
在标记的行上遗漏Set
...
Public Sub GetZipContents()
Dim oApp As Shell32.Shell
Set oApp = New Shell32.Shell
Dim strFile As String
Dim xFname
Dim xRow As Long
Dim newRow As Long
Dim rNew As Range
Dim fileNameInZip, fd
Dim oFolder As Variant
Dim i As Integer
i = 1
xRow = 0
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.Title = "Select the zip to get file names from"
fd.Filters.Clear
fd.Filters.Add "Zip Files", "*.zip"
fd.FilterIndex = 1
If fd.Show = -1 Then
strFile = fd.SelectedItems(1)
Set oFolder = oApp.Namespace(strFile).Items '< Set!
Range("A" & i).Value = strFile
i = i + 1
For Each fileNameInZip In oFolder
Range("A" & i).Value = fileNameInZip
i = i + 1
Next
Set oApp = Nothing
End If
End Sub