2013-10-31 85 views
6

我有檔案的一個系列:C:/archive1.zip,C:/archive2.zip等如何打開從VBA歸檔文件而不解壓存檔

我想只提取一個來自每個檔案的文件。 每個歸檔具有相同的結構和文件可在發現:

C:/archive1.zip/folderlevel1/folderlevel2/folderlevel3/Myfile.csv C:/archive2.zip/folderlevel1/folderlevel2/folderlevel3/Myfile.csv

如何讀取vba中的所有文件Myfile.csv?

謝謝!

+3

請參閱示例2:http://www.rondebruin.nl/win/s7/win002.htm –

回答

9

你可以做到這一點,因爲這:

' 
' UnZip 1 file from a zip file: 
' 
Function entUnZip1File(ByVal strZipFilename, ByVal strDstDir, _ 
    ByVal strFilename) 
' 
    Const glngcCopyHereDisplayProgressBox = 256 
' 
    Dim intOptions, objShell, objSource, objTarget 
' 
' Create the required Shell objects 
    Set objShell = CreateObject("Shell.Application") 
' 
' Create a reference to the files and folders in the ZIP file 
    Set objSource = _ 
    objShell.NameSpace(strZipFilename).Items.item(CStr(strFilename)) 
' 
' Create a reference to the target folder 
    Set objTarget = objShell.NameSpace(strDstDir) 
' 
    intOptions = glngcCopyHereDisplayProgressBox 
' 
' UnZIP the files 
    objTarget.CopyHere objSource, intOptions 
' 
' Release the objects 
    Set objSource = Nothing 
    Set objTarget = Nothing 
    Set objShell = Nothing 
' 
    entUnZip1File = 1 
' 
End Function 

而在你的宏,調用該函數將文件解壓到任何C其中:\ temp目錄或任何目標文件夾,而不是C:\ TEMP:

entUnZip1File "C:\archive1.zip", "C:\temp", "folderlevel1/folderlevel2/folderlevel3/Myfile.csv" 
+0

非常感謝Jacouh。它工作得很好。事實上,這比我想象的要容易。我想我沒有理解Object.Namespace函數的強大功能! – LouInNY