问题描述
此代码存在一些问题
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
如果我在项目内单击运行(F5),则它运行正常,但是当我尝试通过按钮快捷方式调用它时,它不会捕获SendKeys
.如果我尝试逐步(F8)运行它,也会发生这种情况
BTW %T%n%C
用于西班牙语命令组合(所有图像,不压缩,不改变分辨率,不删除裁剪区域)
我使用SendKeys
的原因是我试图删除函数中的裁剪区域以便从按钮调用它,所以我可以跳过标记选项.据我所知,对象模型中没有任何东西可以做到这一点.
我错过了重点吗?
不确定您实际要实现的目标,但是通常在VBA中通常避免使用SendKeys()
方法,因为该方法不可靠且充斥着错误. /p>
SendKeys()
会将虚拟按键发送到执行时关注的任何窗口-因此计时就是一切.
如果您知道窗口标题中的精确文本,则可以在使用SendKeys()
AppActivate()
方法强制聚焦.此外,SendKeys()
被更广泛地视为最终尝试"或解决方法,因为您有90%的时间可以使用 winapi 才能可靠地获得相同的结果
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)
The reason I'm using SendKeys
is that I'm trying to remove cropped areas within a function in order to call it from a button, so I could skip marking the options. As far as I know, there's nothing in the object model that allows this.
Am I missing something with the focus?
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
这篇关于VBA MS-WORD sendkeys出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!