Excel VBA - 从.zip文件读取.txt
问题描述:
我想在VBA中创建一个宏,但我是VBA中的新成员。Excel VBA - 从.zip文件读取.txt
我需要开几个.zip
文件,查看特定.txt
写这是什么.txt
文件到Excel的内部,.zip
的名称将在同一行中的Excel,例如:
第一行是.zip
文件的名称,第一行和第二列将是.txt
文件的内容。
我已经有部分的代码,但它不工作的话代码错误91
Sub Text()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String
Dim strDate As String
Dim I As Long
Dim num As Long
Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
MultiSelect:=True)
If IsArray(Fname) = False Then
'Do nothing
Else
'Root folder for the new folder.
'You can also use DefPath = "C:\Users\Ron\test\"
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
For Each fileNameInZip In oApp.Namespace(Fname).Items
If LCase(fileNameInZip) Like LCase("md5.txt") Then
'Open "md5.txt" For Input As #1
'Do Until EOF(1)
'Line Input #1, textline
' text = text & textline
' Loop
' Close #1
' Range("B1").Value = Mid(text, 1, 32)
' Range("A1").Value = Dir(Fname)
End If
Next
End If
End Sub
我不知道,如果这一切都错了还是不行,我已经尝试做一个循环,打开每个文件中的每个文件md5.txt,我必须打开并采取md5.txt中的内容
你能帮助我吗?谢谢。
答
下面是一个循环遍历单元格并获取zip文件,提取内容和读取文件的示例。您可能需要调整zip文件的路径,否则它将默认启动excel文档。如果将整个路径放在A列中的zip中,则无需进行调整。
编辑是为了反映文件md5.txt的名称并将内容放入第二列。
Sub GetData()
Dim iRow As Integer 'row counter
Dim iCol As Integer 'column counter
Dim savePath As String 'place to save the extracted files
Dim fileContents As String 'contents of the file
Dim fso As FileSystemObject 'FileSystemObject to work with files
iRow = 1 'start at first row
iCol = 1 'start at frist column
'set the save path to the temp folder
savePath = Environ("TEMP")
'create the filesystem object
Set fso = New FileSystemObject
Do While ActiveSheet.Cells(iRow, iCol).Value <> ""
fileContents = fso.OpenTextFile(UnzipFile(savePath, ActiveSheet.Cells(iRow, iCol).Value, "md5.txt"), ForReading).ReadAll
ActiveSheet.Cells(iRow, iCol + 1).Value = fileContents
iRow = iRow + 1
Loop
'free the memory
Set fso = Nothing
End Sub
Function UnzipFile(savePath As String, zipName As String, fileName As String) As String
Dim oApp As Shell
Dim strFile As String
'get a shell object
Set oApp = CreateObject("Shell.Application")
'check to see if the zip contains items
If oApp.Namespace(zipName).Items.Count > 0 Then
Dim i As Integer
'loop through all the items in the zip file
For i = 0 To oApp.Namespace(zipName).Items.Count - 1
'check to see if it is the txt file
If UCase(oApp.Namespace(zipName).Items.Item(i)) = UCase(filename) Then
'save the files to the new location
oApp.Namespace(savePath).CopyHere oApp.Namespace(zipName).Items.Item(i)
'set the location of the file
UnzipFile = savePath & "\" & fileName
'exit the function
Exit Function
End If
Next i
End If
'free memory
Set oApp = Nothing
End Function
+0
我有自动生成的压缩文件我需要访问,他们有时会损坏或超大。 我可以检查他们的大小,但有没有办法检查是否可以访问zip文件? – Divin3
看到这里使用VBA http://www.rondebruin.nl/win/s7/win002.htm –
@TimWilliams那不是我们在这里为zip文件的工作。 *请*不要为没有做出任何努力的人提供完整的解决方案。请仅以代码**的主要部分**发布问题的答案(即从zip文件中读取txt的主要功能)。 – cybermonkey
我认为我们可以单独决定我们在这里做什么 - 至少我保留这个选项......我发布的链接绝不是一个完整的解决方案,但是如果它有助于OP,那么我没有这是一个问题。然而,一个链接通常与我准备为一个没有任何努力或不包含任何现有代码的问题一样努力,除非这是我以前没有尝试过的,并且对我来说很有意思。 –