PDA

View Full Version : Solved: Help with SendKeys



wolf.stalker
09-23-2008, 10:55 PM
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


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

GTO
09-23-2008, 11:44 PM
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.



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

wolf.stalker
09-24-2008, 01:19 AM
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


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


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...

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

GTO
09-24-2008, 03:09 AM
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.

wolf.stalker
09-24-2008, 03:26 AM
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.