问题
I'm having some issue with this code
Private Sub CortarSobrantes()
'Procedimiento que llama al comando "Comprimir imágenes" con parámetros
With Application.CommandBars.FindControl(ID:=6382)
SendKeys "%T%n%C{ENTER}", False ' Las letras equivalen a los accesos de teclado en la ventana, ~ para ejecucion
.Execute
End With
End Sub
If I hit Run (F5) inside the project, it runs OK, but when I try to call it from a button shortcut it doesn't catch the sendkeys. This also happens if I try to run it step by step (F8)
BTW %T%n%C
is for spanish command combination (all images, not compress, without resolution change and deleting cropped areas)
Am I missing something with the focus?
回答1:
Not sure what you're actually trying to achieve, but in general the SendKeys()
method is usually avoided in VBA because it's unreliable and buggy at best.
SendKeys()
will send virtual keystrokes to whichever window has focus at the time of execution - so timing is everything.
If you know the exact text in the caption of your window you can use the AppActivate()
method to force focus just before using SendKeys()
Moreover, SendKeys()
is more widely regarded as a "final attempt" or workaround because 90% of the time you can use winapi to get the same result reliably although more advanced knowledge of VBA/programming is required when using Win API
来源:https://stackoverflow.com/questions/33826631/vba-ms-word-trouble-with-sendkeys