Hello guys,
I'm completely new to VBA. My task is I have a certain zip file on my Downloads, I should unzip the file.
With file name known, I'm able to unzip it but the problem is : I don't know the entire file name ("xxx-065-xxx"). I've tried the code below. It indeed shows me that the file exists but doesn't show me the correct full path ("D:\Users\~~~\Downloads\test\*.zip") and hence don't unzip it (Fname is undefined). Could you please help me?
Sub Unzip()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As Variant
Dim strDate As String
Dim myValue As Variant
Fname = "D:\Users\~~~\Downloads\test\*.zip"
If Len(Dir(Fname, vbDirectory)) > 0 Then
Range("A5") = "Exists!"
Else
Range("A5") = "Doesn't exist!"
End If
Range("A6") = Fname
If Fname = False Then
'Do nothing
Else
DefPath = "D:\Users\~~~\Downloads"
If Right(DefPath, 1) <> "" Then
DefPath = DefPath & ""
End If
'Create the folder name
strDate = Format(Now, "dd-mm-yy h-mm-ss")
FileNameFolder = DefPath & "MyUnzipFolder_" & strDate & ""
'Make the normal folder in DefPath
MkDir FileNameFolder
'Extract the files into the newly created folder
Range("A1") = Fname
Range("A2") = DefPath
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
On Error Resume Next
MsgBox "You find the files here: " & FileNameFolder
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub
I'm completely new to VBA. My task is I have a certain zip file on my Downloads, I should unzip the file.
With file name known, I'm able to unzip it but the problem is : I don't know the entire file name ("xxx-065-xxx"). I've tried the code below. It indeed shows me that the file exists but doesn't show me the correct full path ("D:\Users\~~~\Downloads\test\*.zip") and hence don't unzip it (Fname is undefined). Could you please help me?
Sub Unzip()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As Variant
Dim strDate As String
Dim myValue As Variant
Fname = "D:\Users\~~~\Downloads\test\*.zip"
If Len(Dir(Fname, vbDirectory)) > 0 Then
Range("A5") = "Exists!"
Else
Range("A5") = "Doesn't exist!"
End If
Range("A6") = Fname
If Fname = False Then
'Do nothing
Else
DefPath = "D:\Users\~~~\Downloads"
If Right(DefPath, 1) <> "" Then
DefPath = DefPath & ""
End If
'Create the folder name
strDate = Format(Now, "dd-mm-yy h-mm-ss")
FileNameFolder = DefPath & "MyUnzipFolder_" & strDate & ""
'Make the normal folder in DefPath
MkDir FileNameFolder
'Extract the files into the newly created folder
Range("A1") = Fname
Range("A2") = DefPath
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
On Error Resume Next
MsgBox "You find the files here: " & FileNameFolder
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub