1

I need to open a few .zip files, view a specific .txt and write what's inside of this .txt file to an Excel workbook, and the name of the .zip will be in the same row in Excel.

Example:

The first row is the name of the .zip file and in the first row and second column will be the content of the .txt file.

enter image description here

I have part of the code. It says code error 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

I tried to make a loop to open every file md5.txt in every zip that I have to open and take what's inside of the md5.txt

Community
  • 1
  • 1
R.Bacaro
  • 25
  • 1
  • 5
  • 2
    See here for working with zip files using VBA http://www.rondebruin.nl/win/s7/win002.htm – Tim Williams Mar 02 '16 at 20:46
  • 1
    @TimWilliams That's not what we're here for. *Please* don't provide a complete solution for someone that hasn't made any effort. Please post an answer to the question with the **main part of the code only** (i.e, the main function for reading txt from zip files). – AStopher Mar 02 '16 at 20:53
  • 2
    I think we're allowed to decide individually what we're here for - at least I reserve that option... The link I posted is by no means a complete solution, but if it helps the OP then I don't have a problem with that. However a link is typically as much effort as I'm prepared to put in for a question which shows no effort or doesn't include any existing code, unless it's something I've not tried to do before and is interesting to me to solve. – Tim Williams Mar 02 '16 at 21:07
  • @timWilliams Thanks. *I* appreciate the link! – Jerry Jeremiah Mar 02 '16 at 21:10
  • 1
    @cybermonkey Please don't suggest that we shouldn't help someone by posting a link because they "haven't made any effort" and then follow up with "you should instead go out of your way to post a complete answer." That makes no sense. – user1274820 Mar 03 '16 at 07:33
  • 1
    Hi guys, i've already try to make this I'll post the code by editing my question thanks – R.Bacaro Mar 03 '16 at 16:46
  • Sorry for not posting the code before, Cybermonkey. But I wouldn't post something without no effort. Thanks @TimWilliams, I've already look in that link, part of my code belongs to the author heheheh, thank you – R.Bacaro Mar 03 '16 at 16:55
  • Error is on which line? – Tim Williams Mar 03 '16 at 16:59
  • @TimWilliams error in the lines commented( line 19 to 27), when I've tried use the open .txt and write what is inside the .txt to an excel – R.Bacaro Mar 03 '16 at 17:25
  • You haven't set `oApp` to anything. Also make sure to use Variants for paths when dealing with oApp. Check Ron's page again. – Tim Williams Mar 03 '16 at 18:03
  • I have set oApp but and use Variants to pass the text inside the .txt, but I get the same error some of the code I've modified bellow: Set oApp = CreateObject("Shell.Application") For Each fileNameInZip In oApp.Namespace(Fname).items Open "md5.txt" For Input As #1 Do Until EOF(1) Line Input #1, textline text = text & textline Loop Close #1... – R.Bacaro Mar 03 '16 at 18:41

1 Answers1

5

Here is an example of looping through your cells and getting the zip file, extracting the contents, and reading the file. You may need to adjust the path to the zip file or it will default to what ever file the excel document is started in. If you put the whole path to the zip in column A then you would not need to make an adjustment.

Edit was made to reflect the name of the file md5.txt and place contents in second column.

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
Sorceri
  • 7,870
  • 1
  • 29
  • 38
  • 1
    I have auto generated zip files that I need to access and they sometime get corrupted or oversized. I can check them for size, but is there a way to check if the zip file is accessible? – Divin3 Jan 20 '17 at 08:09