YasserKhalil
Well-Known Member
Hello everyone
I have found the following piece of code that is supposed to lock folder
I tested it but the folder is not locked .. it is still available
Any idea how to lock and unlock folder using VBA?
I have found the following piece of code that is supposed to lock folder
Code:
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal PassZero As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal PassZero As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal Operation As String, ByVal Filename As String, Optional ByVal Parameters As String, Optional ByVal Directory As String, Optional ByVal WindowStyle As Long = vbMinimizedFocus) As Long
Private Const GENERIC_ALL = &H1
Private Const OPEN_EXISTING = 3
Private Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000
Sub Lock_Folder()
Dim LockFolderPath As String, fHandle As Long
LockFolderPath = ThisWorkbook.Path & "\TestFolder\"
If LockFolderPath <> "" Then
fHandle = CreateFile(LockFolderPath, GENERIC_ALL, ByVal 0&, ByVal 0&, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, ByVal 0&)
ThisWorkbook.Sheets(1).Cells(1, 2) = fHandle
End If
MsgBox "Folder Locked...", 64
End Sub
Sub Unlock_Folder()
Dim fHandle As Long
fHandle = ThisWorkbook.Sheets(1).Cells(1, 2)
CloseHandle fHandle
End Sub
I tested it but the folder is not locked .. it is still available
Any idea how to lock and unlock folder using VBA?