Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type MSLLHOOKSTRUCT
pt As POINTAPI
mouseData As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Type TVITEM
mask As Long
hItem As LongPtr
state As Long
stateMask As Long
pszText As String
cchTextMax As Long
iImage As Long
iSelectedImage As Long
cChildren As Long
lParam As LongPtr
End Type
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As LongPtr, ByVal hMod As LongPtr, ByVal dwThreadId As Long) As LongPtr
Private Declare PtrSafe Function CallNextHookEx Lib "user32" (ByVal hHook As LongPtr, ByVal nCode As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As LongPtr) As LongPtr
Private Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare PtrSafe Function GetDlgItem Lib "user32" (ByVal hDlg As LongPtr, ByVal nIDDlgItem As Long) As LongPtr
Private Declare PtrSafe Function SetProp Lib "user32" Alias "SetPropA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal hData As LongPtr) As Long
Private Declare PtrSafe Function GetProp Lib "user32" Alias "GetPropA" (ByVal hwnd As LongPtr, ByVal lpString As String) As LongPtr
Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
Private Declare PtrSafe Function SendMessageByRef Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Private Declare PtrSafe Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As String) As LongPtr
Private hHook As LongPtr, hwndVBE As LongPtr, hwndLV As LongPtr, hNode As LongPtr, hChildNode As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Type TVITEM
mask As Long
hItem As Long
state As Long
stateMask As Long
pszText As String
cchTextMax As Long
iImage As Long
iSelectedImage As Long
cChildren As Long
lParam As Long
End Type
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Private Declare Function SetProp Lib "user32" Alias "SetPropA" (ByVal hwnd As Long, ByVal lpString As String, ByVal hData As Long) As Long
Private Declare Function GetProp Lib "user32" Alias "GetPropA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessageByRef Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private hHook As Long, hwndVBE As Long, hwndLV As Long, hNode As Long, hChildNode As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Private Const TV_FIRST = &H1100
Private Const TVM_GETNEXTITEM = (TV_FIRST + 10)
Private Const TVM_SELECTITEM = (TV_FIRST + 11)
Private Const TVM_GETITEM = TV_FIRST + 12
Private Const TVM_EXPAND = TV_FIRST + 2
Private Const TVE_EXPAND = &H2
Private Const TVGN_CARET = &H9
Private Const TVGN_ROOT = &H0
Private Const TVGN_NEXT = &H1
Private Const TVIF_TEXT = &H1
Private Const TVGN_CHILD = &H4
Private Const BM_CLICK = &HF5
Private Const WM_SETTEXT = &HC
Private Const MAX_ITEM = 256
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
Private bFlag As Boolean
Private sVBAProjectPassword As String
Sub Test()
Dim sWorkBookName As String
Dim sVBProjectExistingPassword As String
Dim sNewVBProjectName As String
sWorkBookName = "MyAddin.xla" [B][COLOR=#008000]' <== Change the locked Workbook/Addin name as required.[/COLOR][/B]
sVBProjectExistingPassword = "jaafar" [B][COLOR=#008000]' <== Change the locked VBProject password as required.[/COLOR][/B]
sNewVBProjectName = "projectABC" [B][COLOR=#008000]' <== Change this new VBProject name as required.[/COLOR][/B]
If UnlockAndRenameVBProject(sWorkBookName, sVBProjectExistingPassword, sNewVBProjectName) Then
MsgBox "sucess"
[COLOR=#008000]' If succes,continue with your code here....[/COLOR]
[COLOR=#008000]' ******************************************[/COLOR]
[COLOR=#008000]' Dim LongStr As String[/COLOR]
[COLOR=#008000] ' LongStr = Application.VBE.VBProjects("projectABC").VBComponents("Main").CodeModule.Lines(500, 500)[/COLOR]
End If
End Sub
Private Function UnlockAndRenameVBProject(ByVal WorkbookName As String, ByVal VBProjectPassword As String, ByVal NewVBProjectName As String) As Boolean
Dim sPrevVBProjectName As String, sErrorFeedBack As String
Dim tVI As TVITEM, oWb As Object
On Error GoTo errHandler
bFlag = False
sVBAProjectPassword = VBProjectPassword
Set oWb = Workbooks(WorkbookName)
hwndVBE = FindWindow("wndclass_desked_gsk", vbNullString)
If hwndVBE = 0 Then
Application.VBE.MainWindow.Visible = True
Application.VBE.MainWindow.Visible = False
hwndVBE = FindWindow("wndclass_desked_gsk", vbNullString)
End If
If hwndVBE Then
hwndLV = FindWindowEx(hwndVBE, 0, "PROJECT", vbNullString)
hwndLV = FindWindowEx(hwndLV, 0, "SysTreeView32", vbNullString)
hNode = SendMessage(hwndLV, TVM_GETNEXTITEM, TVGN_ROOT, 0)
Do While hNode <> 0
tVI.hItem = hNode
tVI.mask = TVIF_TEXT
tVI.cchTextMax = MAX_ITEM
tVI.pszText = String(MAX_ITEM, 0)
Call SendMessageByRef(hwndLV, TVM_GETITEM, 0, tVI)
If InStr(1, tVI.pszText, WorkbookName, vbTextCompare) > 0 Then
Call SetHook
hChildNode = SendMessage(hwndLV, TVM_SELECTITEM, TVGN_CARET, ByVal hNode)
hChildNode = SendMessage(hwndLV, TVM_GETNEXTITEM, TVGN_CHILD, ByVal hNode)
Call SendMessage(hwndLV, TVM_EXPAND, TVE_EXPAND, ByVal hChildNode)
hChildNode = SendMessage(hwndLV, TVM_GETNEXTITEM, TVGN_CHILD, hChildNode)
Call SendMessage(hwndLV, TVM_GETNEXTITEM, TVGN_NEXT, hChildNode)
Call SendMessage(hwndLV, TVM_SELECTITEM, TVGN_CARET, ByVal hChildNode)
Call SendMessage(hwndLV, TVM_GETNEXTITEM, TVGN_NEXT, ByVal hChildNode)
Call SendMessage(hwndLV, TVM_EXPAND, TVE_EXPAND, ByVal hChildNode)
sPrevVBProjectName = Application.VBE.ActiveVBProject.Name
DoEvents
Application.VBE.ActiveVBProject.Name = NewVBProjectName
End If
hNode = SendMessage(hwndLV, TVM_GETNEXTITEM, TVGN_NEXT, hNode)
DoEvents
Loop
End If
errHandler:
Call RemoveHook
Select Case Err
Case 9, -2147352565
sErrorFeedBack = "The Workbook or Addin whose name is :" & vbLf & "'" & WorkbookName & "'" & " is not open."
Case 50132
bFlag = True
sErrorFeedBack = "The VBProject : '" & sPrevVBProjectName & "' was successfully unlocked but " & vbLf
sErrorFeedBack = sErrorFeedBack & "failed to be renamed due to the existence of one or more " & vbLf
sErrorFeedBack = sErrorFeedBack & "invalid characters in the new VBProject name that was provided !" & vbLf & vbLf
sErrorFeedBack = sErrorFeedBack & "Remove the invalid character(s) and try again."
Case 50289
sErrorFeedBack = "Incorrect VBAProject Password."
Case 1004
sErrorFeedBack = "Programmatic Access To Visual Basic Is Not Trusted." & vbLf & vbLf
sErrorFeedBack = sErrorFeedBack & "Click to select the Trust access to the VBA project object model check box under 'Macros Security'."
Case 0
UnlockAndRenameVBProject = True
End Select
If sErrorFeedBack <> "" Then
MsgBox sErrorFeedBack, IIf(Err = 50132, vbExclamation, vbCritical), "Error."
End If
End Function
Private Sub SetHook()
hHook = SetWindowsHookEx(WH_CBT, AddressOf HookProc, 0, GetCurrentThreadId)
SetProp Application.hwnd, "hhook", hHook
End Sub
Private Sub RemoveHook()
UnhookWindowsHookEx GetProp(Application.hwnd, "hhook")
End Sub
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Function HookProc(ByVal idHook As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Function HookProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Dim lRet As Long
Dim sBuffer As String
If idHook = HCBT_ACTIVATE Then
sBuffer = Space(256)
lRet = GetClassName(wParam, sBuffer, 256)
If Left(sBuffer, lRet) = "#32770" Then
Call SendMessageByString(GetDlgItem(wParam, &H155E), WM_SETTEXT, 0, ByVal sVBAProjectPassword)
Call SendMessage(GetDlgItem(wParam, 1), BM_CLICK, 0, ByVal 0)
If bFlag Then
Call SendMessage(GetDlgItem(wParam, 2), BM_CLICK, 0, ByVal 0)
End If
bFlag = True
End If
End If
HookProc = CallNextHookEx(hHook, idHook, ByVal wParam, ByVal lParam)
End Function