Consulting

Results 1 to 5 of 5

Thread: Solved: Help with SendKeys

  1. #1

    Solved: Help with SendKeys

    Hey all. I am trying to create a macro that will run on auto open event. i have been reading up on code and send keys but just........can't.........seem to get it to work. the last bit of code i found looks like it should work, but does not :-\

    can anyone help me tweak this to work? i did a similar post months ago and never got it to work, but have come back to it and will try again...

    http://www.vbaexpress.com/forum/showthread.php?t=19628

    [vba]
    Sub LockVBAProject()
    With Application
    '//execute the controls to lock the project\\

    'the first line below ".vbe.commandbars......" is the line in which i get the error.
    'error msg Run-time error '1004', app-defined or obj-def error

    '******************************************************
    .VBE.CommandBars("Menu Bar").Controls("Tools").Controls("VBAProject Properties...").Execute
    '******************************************************

    '//activate 'protection'\\
    .SendKeys "^{TAB}"
    '//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\\
    .SendKeys "{ }"
    '//enter password (password is 123 in this example)\\
    .SendKeys "{TAB}" & "123"
    '//confirm password\\
    .SendKeys "{TAB}" & "123"
    '//scroll down to OK key\\
    .SendKeys "{TAB}"
    '//click OK key\\
    .SendKeys "{ENTER}"

    'the project is now locked - this takes effect
    End With
    End Sub
    [/vba]

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I personally would avoid sendkeys if at all possible, which in this case I believe sendmessage would be better. Of course that wasn't your question, and I don't have the api code handy, so...

    For sendkeys to work, the window has to be visible and have the focus.

    Hence - you need to make the vbe window visible before sending keys to the menubar.



    [VBA]Option Explicit
    Sub LockVBAProject()



    With Application


    .VBE.MainWindow.Visible = True



    '//execute the controls to lock the project\\

    'the first line below ".vbe.commandbars......" is the line in which i get the error.
    'error msg Run-time error '1004', app-defined or obj-def error

    '******************************************************
    .VBE.CommandBars("Menu Bar").Controls("Tools").Controls("VBAProject Properties...").Execute
    '******************************************************

    '//activate 'protection'\\
    .SendKeys "^{TAB}"
    '//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\\
    .SendKeys "{ }"
    '//enter password (password is 123 in this example)\\
    .SendKeys "{TAB}" & "123"
    '//confirm password\\
    .SendKeys "{TAB}" & "123"
    '//scroll down to OK key\\
    .SendKeys "{TAB}"
    '//click OK key\\
    .SendKeys "{ENTER}"

    'the project is now locked - this takes effect

    .VBE.MainWindow.Visible = False
    End With

    End Sub
    [/VBA]

  3. #3
    Quote Originally Posted by GTO

    For sendkeys to work, the window has to be visible and have the focus.

    Hence - you need to make the vbe window visible before sending keys to the menubar.
    well sure and as far as i know, it is visable. both of the examples i have uesed are very similar. the

    [vba]
    .VBE.CommandBars("Menu Bar").Controls("Tools").Controls("VBAProject Properties...").Execute
    [/vba]

    gives me an error and i have also tried using the following base code with a few changes but can not get it to work either...

    [vba]With Application
    .SendKeys "%{F11}", True ' VBE
    .SendKeys "^r", True ' Set focus to Explorer
    .SendKeys "{TAB}", True ' Tab to locked project
    .SendKeys "~", True ' Enter
    .SendKeys "password"
    .SendKeys "~", True ' Enter
    End With [/vba]

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Did you add in the following as shown(?):

    With Application
    .VBE.MainWindow.Visible = True
    ...statements... (your code)
    .VBE.MainWindow.Visible = False
    End With

    You mention that 'as far as you know, the vbe window is visible', so I am curious to know whether you were trying to step-thru the ode from the vbe window itself. If this is the case, this errored for me, as the ...properties dialog becomes focused ans upon stepping thru, the vbe window is now focused. Probably not articulate, but try running the sub from the app (workbook) window. It ran w/o a hitch for me.

  5. #5
    ok..i adding the following

    .vbe.mainwindow.visible = true
    .
    .vbe.mainwindow.visible = false

    so at first, i was getting all kinds of errors (won't mention them as they werea all n00b ones like forgetting to remove a ' {comment} from a line of code that needed to be there like "With Application"). did some research and didn;t know that microshaft xl does not trust the you the publisher and that you have to tell it you are a trusted source :-\ but after that, i think i got it working! "i am so happy i could squirt!" (can't think of movie title but phrase came to mind) thanks for your help. looking back on this, i have been right at the 98% mark for a long time.....thanks for your 2% to draw this to complete.

Posting Permissions

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