Results 1 to 14 of 14

Thread: [Excel] Unlock VBA Project Programmatically using API

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    11

    [Excel] Unlock VBA Project Programmatically using API

    I am attempting to unlock a VBA Project programmatically using a slightly-altered version of the code found here: (http://www.siddharthrout.com/2013/04...that-you-know/).

    As noted, I have made some minor adaptations because I need it to work with the current Application instance and Active workbook, as the workbook in question is a form submitted through a UCM system and does not always have the same name (so I cannot specify a file path).

    The issue I am getting is that the code does not appear to be able to locate the VBAProject Password window. The window appears, and I can enter the password manually, but it should be doing it automatically.

    Any help as to how to resolve this would be greatly appreciated as I am an API novice.

    For reference, here is my altered code with changes highlighted:
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As Any, 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 GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    Private Declare Function GetWindowTextLength Lib "user32" Alias _
    "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Dim Ret As Long, ChildRet As Long, OpenRet As Long
    Dim strBuff As String, ButCap As String
    Dim MyPassword As String
    
    Const WM_SETTEXT = &HC
    Const BM_CLICK = &HF5
    
    Sub UnlockVBA()
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' This code unlocks the VBA Project programmatically in order to allow ListProcedures to run. Adapted from: '
    ' http://www.siddharthrout.com/2013/04/24/unprotecting-vba-project-password-using-a-password-that-you-know/ '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
        Dim xlAp As Object, oWb As Object
    
    '    Set xlAp = CreateObject("Excel.Application")
        Set xlAp = Application
        
        xlAp.Visible = True
    
        '~~> Open the workbook in a separate instance
    '    Set oWb = xlAp.Workbooks.Open("C:\Sample.xlsm")
        Set oWb = xlAp.ActiveWorkbook
    
        '~~> Launch the VBA Project Password window
        '~~> I am assuming that it is protected. If not then
        '~~> put a check here.
    '    xlAp.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
        xlAp.VBE.CommandBars(1).FindControl(ID:=2557, recursive:=True).Execute
                
        '~~> Your passwword to open then VBA Project
        MyPassword = "test"
    
        '~~> Get the handle of the "VBAProject Password" Window
        Ret = FindWindow(vbNullString, "VBAProject Password")
    
        If Ret <> 0 Then
            'MsgBox "VBAProject Password Window Found"
    
            '~~> Get the handle of the TextBox Window where we need to type the password
            ChildRet = FindWindowEx(Ret, ByVal 0&, "Edit", vbNullString)
    
            If ChildRet <> 0 Then
                'MsgBox "TextBox's Window Found"
                '~~> This is where we send the password to the Text Window
                SendMess MyPassword, ChildRet
    
                DoEvents
    
                '~~> Get the handle of the Button's "Window"
                ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)
    
                '~~> Check if we found it or not
                If ChildRet <> 0 Then
                    'MsgBox "Button's Window Found"
    
                    '~~> Get the caption of the child window
                    strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
                    GetWindowText ChildRet, strBuff, Len(strBuff)
                    ButCap = strBuff
    
                    '~~> Loop through all child windows
                    Do While ChildRet <> 0
                        '~~> Check if the caption has the word "OK"
                        If InStr(1, ButCap, "OK") Then
                            '~~> If this is the button we are looking for then exit
                            OpenRet = ChildRet
                            Exit Do
                        End If
    
                        '~~> Get the handle of the next child window
                        ChildRet = FindWindowEx(Ret, ChildRet, "Button", vbNullString)
                        '~~> Get the caption of the child window
                        strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
                        GetWindowText ChildRet, strBuff, Len(strBuff)
                        ButCap = strBuff
                    Loop
    
                    '~~> Check if we found it or not
                    If OpenRet <> 0 Then
                        '~~> Click the OK Button
                        SendMessage ChildRet, BM_CLICK, 0, vbNullString
                    Else
                        MsgBox "The Handle of OK Button was not found"
                    End If
                Else
                     MsgBox "Button's Window Not Found"
                End If
            Else
                MsgBox "The Edit Box was not found"
            End If
        Else
            MsgBox "VBAProject Password Window was not Found"
        End If
    End Sub
    
    Sub SendMess(Message As String, hwnd As Long)
        Call SendMessage(hwnd, WM_SETTEXT, False, ByVal Message)
    End Sub

  2. #2
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: [Excel] Unlock VBA Project Programmatically using API

    I need it to work with the current Application instance and Active workbook, as the workbook in question is a form submitted through a UCM system and does not always have the same name (so I cannot specify a file path).
    as Sid said.....

    What you want can be achieved, however you have to ensure that workbook for which you want to un-protect the VBA has to be opened in a separate Excel Instance.
    In addition...

    Use CreateObject when there is no current instance of the object. If an instance of the object is already running, a new instance is started, and an object of the specified type is created. To use the current instance, or to start the application and have it load a file, use the GetObject function.
    Source; http://msdn.microsoft.com/en-us/libr.../gg264813.aspx

    Code below do the trick, but in a different instance of Excel.

    Code:
    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 GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
     
    Private Declare Function GetWindowTextLength Lib "user32" Alias _
    "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
     
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
     
    Dim Ret As Long, ChildRet As Long, OpenRet As Long
    Dim strBuff As String, ButCap As String
    Dim MyPassword As String
     
    Const WM_SETTEXT = &HC
    Const BM_CLICK = &HF5
     
    Sub UnlockVBA()
    
        Dim xlAp As Object, oWb As Object
        Set xlAp = CreateObject("Excel.Application")
        xlAp.Visible = True
        Dim strpath As String
        
        '~~> Open the workbook in a separate instance
        strpath = ActiveSheet.Range("D1").Value 'InputBox("Enter file path", "file path")
        If Len(strpath) <= 0 Then Exit Sub
        strpath = CStr(strpath)
        Set oWb = xlAp.Workbooks.Open(strpath)
    
        '~~> Launch the VBA Project Password window
        '~~> I am assuming that it is protected. If not then
        '~~> put a check here.
        xlAp.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
     
        '~~> Your password to open then VBA Project
        MyPassword = "Your Password"
     
        '~~> Get the handle of the "VBAProject Password" Window
        Ret = FindWindow(vbNullString, "Your Project name Password")
     
        If Ret <> 0 Then
            'MsgBox "VBAProject Password Window Found"
     
            '~~> Get the handle of the TextBox Window where we need to type the password
            ChildRet = FindWindowEx(Ret, ByVal 0&, "Edit", vbNullString)
     
            If ChildRet <> 0 Then
                'MsgBox "TextBox's Window Found"
                '~~> This is where we send the password to the Text Window
                SendMess MyPassword, ChildRet
     
                DoEvents
     
                '~~> Get the handle of the Button's "Window"
                ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)
     
                '~~> Check if we found it or not
                If ChildRet <> 0 Then
                    'MsgBox "Button's Window Found"
     
                    '~~> Get the caption of the child window
                    strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
                    GetWindowText ChildRet, strBuff, Len(strBuff)
                    ButCap = strBuff
     
                    '~~> Loop through all child windows
                    Do While ChildRet <> 0
                        '~~> Check if the caption has the word "OK"
                        If InStr(1, ButCap, "OK") Then
                            '~~> If this is the button we are looking for then exit
                            OpenRet = ChildRet
                            Exit Do
                        End If
     
                        '~~> Get the handle of the next child window
                        ChildRet = FindWindowEx(Ret, ChildRet, "Button", vbNullString)
                        '~~> Get the caption of the child window
                        strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
                        GetWindowText ChildRet, strBuff, Len(strBuff)
                        ButCap = strBuff
                    Loop
     
                    '~~> Check if we found it or not
                    If OpenRet <> 0 Then
                        '~~> Click the OK Button
                        SendMessage ChildRet, BM_CLICK, 0, vbNullString
                    Else
                        MsgBox "The Handle of OK Button was not found"
                    End If
                Else
                     MsgBox "Button's Window Not Found"
                End If
            Else
                MsgBox "The Edit Box was not found"
            End If
        Else
            MsgBox "VBAProject Password Window was not Found"
        End If
        
    End Sub
     
    Sub SendMess(Message As String, hwnd As Long)
        Call SendMessage(hwnd, WM_SETTEXT, False, ByVal Message)
    End Sub
    As you can notice I place file path in range(D1). Of course, you can use the system you want, in order to get path file.
    Also notice, I am using this line of code

    Code:
    xlAp.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
    Due you has to point to <VBProject's properties windows> instead of <VBProject explorer windows>.

    Infact you'are doing the same that you do via menu>VBPoject properties.
    Basically this code do as follow....
    * Open Project Props win
    * Get the texbox ID and send it via <SendMessage> the password you has set via code.
    * Get the buttom ID and send it via <SendMessage> the click method (BM_CLICK).

    Conclusion: a least for me, this code became usefull, when you plane unprotect several projects at time.

    HTH

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    11

    Re: [Excel] Unlock VBA Project Programmatically using API

    Thanks for the advice. I have now managed to get the following to work, however I have a few minor 'issues' I would like to resolve if possible.

    1) As this opens a new instance of Excel with the current workbook in it (I am only unlocking one project at a time - the one for the current workbook), I want to close the original instance of Excel when the new one opens. I have managed to accomplish this by inserting an Application.Quit call at the end of the code (highlighted below). However, this causes an issue in that the new instance of the file is Read-Only for about a minute before popping up the standard "this file is now available for editing, do you wish to open as Read-Write" message. Is there a way to get around this?

    2) Is there an easy way to automatically close the VBA Project Properties window automatically? At the moment I am having to manually click OK/Cancel before it closes, which is why I was originally trying to use a different commandbar option.

    Code:
    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 GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    Private Declare Function GetWindowTextLength Lib "user32" Alias _
    "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Dim Ret As Long, ChildRet As Long, OpenRet As Long
    Dim strBuff As String, ButCap As String
    Dim MyPassword As String
    
    Const WM_SETTEXT = &HC
    Const BM_CLICK = &HF5
    
    Sub UnlockVBA()
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' This code unlocks the VBA Project programmatically in order to allow ListProcedures to run. Adapted from: '
    ' http://www.siddharthrout.com/2013/04/24/unprotecting-vba-project-password-using-a-password-that-you-know/ '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
        Dim xlAp As Object, oWb As Object
    
        Set xlAp = CreateObject("Excel.Application")
        
        xlAp.Visible = True
    
        '~~> Open the workbook in a separate instance
        Set oWb = xlAp.Workbooks.Open(Application.ActiveWorkbook.FullName)
    
        '~~> Launch the VBA Project Password window
        '~~> I am assuming that it is protected. If not then
        '~~> put a check here.
        xlAp.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
                
        '~~> Your passwword to open then VBA Project
        MyPassword = "test"
    
        '~~> Get the handle of the "VBAProject Password" Window
        Ret = FindWindow(vbNullString, "VBAProject Password")
    
        If Ret <> 0 Then
            'MsgBox "VBAProject Password Window Found"
    
            '~~> Get the handle of the TextBox Window where we need to type the password
            ChildRet = FindWindowEx(Ret, ByVal 0&, "Edit", vbNullString)
    
            If ChildRet <> 0 Then
                'MsgBox "TextBox's Window Found"
                '~~> This is where we send the password to the Text Window
                SendMess MyPassword, ChildRet
    
                DoEvents
    
                '~~> Get the handle of the Button's "Window"
                ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)
    
                '~~> Check if we found it or not
                If ChildRet <> 0 Then
                    'MsgBox "Button's Window Found"
    
                    '~~> Get the caption of the child window
                    strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
                    GetWindowText ChildRet, strBuff, Len(strBuff)
                    ButCap = strBuff
    
                    '~~> Loop through all child windows
                    Do While ChildRet <> 0
                        '~~> Check if the caption has the word "OK"
                        If InStr(1, ButCap, "OK") Then
                            '~~> If this is the button we are looking for then exit
                            OpenRet = ChildRet
                            Exit Do
                        End If
    
                        '~~> Get the handle of the next child window
                        ChildRet = FindWindowEx(Ret, ChildRet, "Button", vbNullString)
                        '~~> Get the caption of the child window
                        strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
                        GetWindowText ChildRet, strBuff, Len(strBuff)
                        ButCap = strBuff
                    Loop
    
                    '~~> Check if we found it or not
                    If OpenRet <> 0 Then
                        '~~> Click the OK Button
                        SendMessage ChildRet, BM_CLICK, 0, vbNullString
                    Else
                        MsgBox "The Handle of OK Button was not found"
                    End If
                Else
                     MsgBox "Button's Window Not Found"
                End If
            Else
                MsgBox "The Edit Box was not found"
            End If
        Else
            MsgBox "VBAProject Password Window was not Found"
        End If
        
        Application.DisplayAlerts = False
        Application.Quit
       
    End Sub
    
    Sub SendMess(Message As String, hwnd As Long)
        Call SendMessage(hwnd, WM_SETTEXT, False, ByVal Message)
    End Sub

  4. #4
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Smile Re: [Excel] Unlock VBA Project Programmatically using API

    PHP Code:
    I want to close the original instance of Excel when the new one opens 
    This modified piece of code, do the trick....

    Code:
    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 GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
     
    Private Declare Function GetWindowTextLength Lib "user32" Alias _
    "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
     
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
     
    Dim Ret As Long, ChildRet As Long, OpenRet As Long
    Dim strBuff As String, ButCap As String
    Dim MyPassword As String
     
    Const WM_SETTEXT = &HC
    Const BM_CLICK = &HF5
     
    Sub UnlockVBA()
    
        Dim xlAp As Object, oWb As Object, sWb As Object
        strpath = ActiveSheet.Range("D1").Value
        Set xlAp = CreateObject("Excel.Application")
        xlAp.Visible = True
        Set sWb = ThisWorkbook
    
        DetectExcel
    
        strpath = ActiveSheet.Range("D1").Value
        Set oWb = xlAp.Workbooks.Open(strpath)
        oWb.Activate
        xlAp.Parent.Windows(1).Visible = True
        
    
        '~~> Launch the VBA Project Password window
        '~~> I am assuming that it is protected. If not then
        '~~> put a check here.
        xlAp.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True, Visible:=False).Execute
     
        '~~> Your passwword to open then VBA Project
        MyPassword = "YOUR PASSWORD HERE"
     
        '~~> Get the handle of the "VBAProject Password" Window
        Ret = FindWindow(vbNullString, "Your Project name Password here")
     
        If Ret <> 0 Then
            'MsgBox "VBAProject Password Window Found"
     
            '~~> Get the handle of the TextBox Window where we need to type the password
            ChildRet = FindWindowEx(Ret, ByVal 0&, "Edit", vbNullString)
     
            If ChildRet <> 0 Then
                'MsgBox "TextBox's Window Found"
                '~~> This is where we send the password to the Text Window
                SendMess MyPassword, ChildRet
     
                DoEvents
     
                '~~> Get the handle of the Button's "Window"
                ChildRet = FindWindowEx(Ret, ByVal 0&, "Button", vbNullString)
     
                '~~> Check if we found it or not
                If ChildRet <> 0 Then
                    'MsgBox "Button's Window Found"
     
                    '~~> Get the caption of the child window
                    strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
                    GetWindowText ChildRet, strBuff, Len(strBuff)
                    ButCap = strBuff
     
                    '~~> Loop through all child windows
                    Do While ChildRet <> 0
                        '~~> Check if the caption has the word "OK"
                        If InStr(1, ButCap, "Aceptar") Then
                            '~~> If this is the button we are looking for then exit
                            OpenRet = ChildRet
                            Exit Do
                        End If
     
                        '~~> Get the handle of the next child window
                        ChildRet = FindWindowEx(Ret, ChildRet, "Button", vbNullString)
                        '~~> Get the caption of the child window
                        strBuff = String(GetWindowTextLength(ChildRet) + 1, Chr$(0))
                        GetWindowText ChildRet, strBuff, Len(strBuff)
                        ButCap = strBuff
                    Loop
     
                    '~~> Check if we found it or not
                    If OpenRet <> 0 Then
                        '~~> Click the OK Button
                        SendMessage ChildRet, BM_CLICK, 0, vbNullString
                    Else
                        MsgBox "The Handle of OK Button was not found"
                    End If
                Else
                     MsgBox "Button's Window Not Found"
                End If
            Else
                MsgBox "The Edit Box was not found"
            End If
        Else
            MsgBox "VBAProject Password Window was not Found"
        End If    
    
            Application.DisplayAlerts = False
            Application.Quit
    
    End Sub
     
    Sub SendMess(Message As String, hWnd As Long)
        Call SendMessage(hWnd, WM_SETTEXT, False, ByVal Message)
    End Sub
    
    
    Sub DetectExcel()
    
    ' The procedure detects that it is running Excel and registers it.
        Const WM_USER = 1024
        Dim hWnd As Long
    ' If Excel is running this API call returns the controller.
        hWnd = FindWindow("XLMAIN", 0)
        If hWnd = 0 Then    ' 0 mean that Excel is not running.
            Exit Sub
        Else
    
        'Excel is running, so the SendMessage API function is used
         'To enter it into the Running Object table.
            SendMessage hWnd, WM_USER + 18, 0, 0
        End If
    
    End Sub
    Is there an easy way to automatically close the VBA Project Properties window automatically?
    As you can notice you does not see the password dialog windows.
    Now you has to ask yourself, ¿Why? and ¿How it do it?, And what's more important, I can use the same technique to close this dialog?. Remember the mean steps to get it:

    '~~> Get the handle of the "VBAProject Password" Window
    '~~> Get the handle of the Button's "Window"
    '~~> Check if we found it or not
    '~~> Click the OK Button

    Remember the prize for achieving this is to avoid having to close the dialog box manually. It's a good challenge, and does not affect the operation of the program, it is only a bonus.


    HTH

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    11

    Re: [Excel] Unlock VBA Project Programmatically using API

    Quote Originally Posted by 3com View Post
    This modified piece of code, do the trick....
    Thank you for the response. Unfortunately, it does not resolve the issue I am having (highlighted below).

    Quote Originally Posted by AerosAtar View Post
    1) As this opens a new instance of Excel with the current workbook in it (I am only unlocking one project at a time - the one for the current workbook), I want to close the original instance of Excel when the new one opens. I have managed to accomplish this by inserting an Application.Quit call at the end of the code (highlighted below). However, this causes an issue in that the new instance of the file is Read-Only for about a minute before popping up the standard "this file is now available for editing, do you wish to open as Read-Write" message. Is there a way to get around this?
    Does anyone have any further advice on how I can get around this? I have tried closing the workbook before calling the Application.Quit, but that just stops the code dead in its tracks and the original Excel instance remains open...

  6. #6
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: [Excel] Unlock VBA Project Programmatically using API

    Keep the target book closed, when you run this code.
    otherwise, you will get a read-only instance.

    This file contains two books. One called TESTBOOK and as the name suggests the book is the target, and whose code is password protected. (the PASSWORD is in the code)
    The other book called UnlockingPW contains macros and a command button that runs them.
    Unzip this file to C: \, if you save it to another path, please modify your route on the worksheet called Mainsheet in Range (d1).

    Try this file without making any changes. it works, atleast 4 me.
    Attached Files Attached Files

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    11

    Re: [Excel] Unlock VBA Project Programmatically using API

    Quote Originally Posted by 3com View Post
    Keep the target book closed, when you run this code.
    otherwise, you will get a read-only instance.
    The issue here is that the project I want to unprotect belongs to the current workbook. I am not calling the path using a cell within a worksheet - I am using a call to Application.ActiveWorkbook.FullName to obtain the filepath.

    I would really like the code to sit within the workbook whose project is being unlocked - I do not want a separate 'administration' workbook, if possible.

  8. #8
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: [Excel] Unlock VBA Project Programmatically using API

    I would really like the code to sit within the workbook whose project is being unlocked
    This looks like you want open a closed box with a key that is inside of the closed box. Perhaps I do not get right your point, if so, please excuse me.
    My project already unlock target project, and get non read-only instance. You can integrate my code to your project, but....

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [Excel] Unlock VBA Project Programmatically using API

    Seems like I missed the party :P

    Ok, AerosAtar, I went through the posts but ended up getting confused as to what you are exactly trying. Forget the code for a moment... Can you explain in simple steps as to what exactly are you trying to achieve?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    11

    Re: [Excel] Unlock VBA Project Programmatically using API

    To explain in as simple a way as I can (I tend to struggle to keep things 'simple' ):

    I have a form that has a lot of VBA code sitting behind it in order to accomplish certain tasks (button clicks, a more powerful concatenation method, resetting button locations, etc, as well as a huge, complicated piece that validates the completion state of the form). In the currently published version of the form, the VBA Project is locked to prevent curious users from seeing/tampering with the code and this is not an issue.

    However, I now need to add an Administration page to in order to assist other, less technically skilled, members of my team with editing the form (not completing it, but editing the form itself). This is a requirement I have been given by management as they do not want a 'single point of failure'.

    As part of the administration page, I am using some of the code found here (http://www.cpearson.com/excel/vbe.aspx) in order to list out all of the procedures within the ThisWorkbook and Module# code modules. This code requires that the project is unlocked and needs to run when the worksheet is first initialised, so I have been looking at a way of unlocking the code programmatically.

    I have attached the workbook I am using to build the administration page in case that is also helpful. Note that this doesn't yet do the initial AD check on opening - I am waiting to add that until everything else works - and the test Workbook_Open() procedure is disabled as otherwise it ends up in an endless loop (hence why I wanted to work in the current Excel instance). Password is "test" in all instances.

    Hopefully that explains it, but please do ask if there is anything I haven't explained very well.
    Attached Files Attached Files

  11. #11
    Addicted Member 3com's Avatar
    Join Date
    Jul 2013
    Location
    Spain
    Posts
    253

    Re: [Excel] Unlock VBA Project Programmatically using API

    Well, try this one...


    Code:
    Sub UnLockMyProj()
    
        Pw = "test"
        Dim MyProj As Object
        Set MyProj = ThisWorkbook.MyProject
        If MyProj.Protection <> 1 Then Exit Sub ' already unprotected - ByVal Pwd As String
        Set Application.VBE.ActiveMy
    Project = MyProj
        SendKeys Pw & "~~"
        Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
        
    End Sub
    Run this macro via Menu>Macros.....
    Or call macro via commandButtom.

    HTH

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    11

    Re: [Excel] Unlock VBA Project Programmatically using API

    I want to avoid using SENDKEYS if at all possible due to the unreliability of this method (i.e. if another window happens to grab focus while the code is executing, it fails).

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [Excel] Unlock VBA Project Programmatically using API

    I am out at the moment and hence cannot see the attachments. Once I get back home I will have a look and reply accordingly.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    11

    Re: [Excel] Unlock VBA Project Programmatically using API

    Hi Sid,

    Just wondering if you have had a chance to look at this yet?

    No worries if not - I still have a fair amount of work to do on 'tidying up' the form/code, so there isn't a massive rush on this at my end.
    Last edited by AerosAtar; Mar 28th, 2014 at 10:34 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width