Results 1 to 4 of 4

Thread: Sendkey Problem

  1. #1
    Join Date
    Sep 2007
    Location
    Global Village
    Posts
    185

    Unanswered: Sendkey Problem

    Hi folks,
    my db exports data to excel and open the excel vba project and write some code in there which activate some links.
    I finally, before saving the excel file, try to password protect the vba project using Sendkey command.
    everything works fine with no error but when I open the excel file later, vba project is not protected.
    here is a part of code which is for Sendkey:
    Code:
         With exApp
    
               '//execute the controls to lock the project\\
               .VBE.CommandBars("Menu Bar").Controls("Tools") _
                           .Controls("VBAProject Properties...").Execute
    
               '//activate 'protection'\\
               .SendKeys "^{TAB}" & " ", True
               '//CAUTION: this either checks OR UNchecks the\\
               '//"Lock Project for Viewing" checkbox, if it's already\\
               '//been locked for viewing, then this will UNlock it\\
    
               '//enter password (password is 123 in this example)\\
               .SendKeys "{TAB}" & "x123", True
    
               '//confirm password\\
               .SendKeys "{TAB}" & "x123", True
    
               '//click OK key\\
                       .SendKeys "{ENTER}", True
    
         End With
    ps:I want to protect vba project in order to prevent users to mess with code.
    <<Never Walk on the Traveled Path, Because it only Leads you to where the Others have been.>>
    Graham Bell

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I have successfully used the below in a production system. SendKeys is a hacky, nasty way to do anything - always use an alternative if one exists.
    Setting VBA Project Password
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Sep 2007
    Location
    Global Village
    Posts
    185
    Thanks Pootle for immediate reply, I put the code in my db but it doesn't work. do you think it is due to I use office 2007?
    also when I open excel file created with this code I receive this message:
    the file you are trying to open is in a different format than specified by file extension. verify that the file is not corrupted and is from a trusted source before opening the file
    and vba project is not locked as well

    ps: I tracked down the execution of code line by line and found the problem which is in set focus on OK button and click it I mean this part of code:
    'OK button
    SetFocusAPI hWndOK
    SendMessage hWndOK, BM_CLICK, 0&, 0&
    it shall be something wrong with office 2007 in this line of code.
    any idea?
    thanks
    Last edited by Aran1; 08-24-10 at 03:59.
    <<Never Walk on the Traveled Path, Because it only Leads you to where the Others have been.>>
    Graham Bell

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    It's not Office 2007 - I have it running in that - although I am running it in compatibility mode (.xls extension).
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

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