VBAEXCELNew
New Member
- Joined
- Apr 3, 2023
- Messages
- 38
- Office Version
- 365
- 2021
- Platform
- Windows
Hi,
i wanted to do an Unzip file without using the references from Shell and Automation however currently i am facing issue of doing late blind then a early blind as early blind work in unzip my files
Here is my vba code
i wanted to do an Unzip file without using the references from Shell and Automation however currently i am facing issue of doing late blind then a early blind as early blind work in unzip my files
Here is my vba code
VBA Code:
Sub UnzipAll()
'Define Variable Data Types
Dim sourceFolder As String
Dim destinationFolder As String
Dim objZipItems As FolderItems
Dim objZipItem As FolderItem
' Get the current user's username
userName = Environ("USERNAME")
'Set the source and destination folders
sourceFolder = "C:\Users\" & userName & "\Desktop\Price checker for FA\"
destinationFolder = "C:\Users\" & userName & "\Desktop\Price checker for FA\"
'Early Binding Reference
'Add Tools -> Reference -> "Microsoft Shell Controls & Automation"
Dim wShApp As Shell
Set wShApp = CreateObject("Shell.Application")
' Check if there are any zip files in the source folder
If Dir(sourceFolder & "*.zip") = "" Then
MsgBox "No zip files found in the source folder."
Exit Sub
End If
'Loop through all ZIP files in the source folder
Dim file As Variant
file = Dir(sourceFolder & "*.zip")
Do While file <> ""
'Set Zip File Name
Dim zipFileName As String
zipFileName = sourceFolder & file
' Check if the file path is valid before attempting to extract its contents
If Len(Dir(zipFileName)) = 0 Then
MsgBox "Invalid file path: " & zipFileName
Exit Sub
End If
'Extract: Unzip all Files to Folder
Set objZipItems = wShApp.Namespace(zipFileName).Items
wShApp.Namespace(destinationFolder).CopyHere objZipItems
'Move to the next file
file = Dir
Loop
MsgBox "All ZIP files extracted successfully."
End Sub