简体   繁体   English

VBA MS-WORD sendkeys 问题

[英]VBA MS-WORD Trouble with sendkeys

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 .如果我在项目中点击 Run (F5),它运行正常,但是当我尝试从按钮快捷方式调用它时,它不会捕获SendKeys This also happens if I try to run it step by step (F8)如果我尝试逐步运行它,也会发生这种情况(F8)

BTW %T%n%C is for Spanish command combination (all images, not compress, without resolution change and deleting cropped areas) BTW %T%n%C用于西班牙语命令组合(所有图像,不压缩,不更改分辨率和删除裁剪区域)

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.我使用SendKeys的原因是我试图删除函数中的裁剪区域以便从按钮调用它,所以我可以跳过标记选项。 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.不确定您实际想要实现的目标,但通常在 VBA 中通常会避免使用SendKeys()方法,因为它充其量是不可靠且有问题的。

SendKeys() will send virtual keystrokes to whichever window has focus at the time of execution - so timing is everything. SendKeys()将虚拟击键发送到执行时具有焦点的任何窗口 - 所以时间就是一切。

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()如果您知道窗口标题中的确切文本,您可以在使用SendKeys()之前使用AppActivate()方法强制聚焦

Moreover, SendKeys() is more widely regarded as a "final attempt" or workaround because 90% of the time you can use to get the same result reliably although more advanced knowledge of VBA/programming is required when using Win API此外, SendKeys()被更广泛地视为“最终尝试”或解决方法,因为 90% 的时间您都可以使用可靠地获得相同的结果,尽管在使用 Win API 时需要更高级的 VBA/编程知识

I'm also having trouble with the SendKeys-statement.我也遇到了 SendKeys 语句的问题。
I'm trying to send Alt= to a Word document.我正在尝试将 Alt= 发送到 Word 文档。 Thereby changing some mahematical formula in an equation field.从而改变方程域中的一些数学公式。 For instance: ((x_1 q+x_2 p)/(p+q),(y_1 q+y_2 p)/(p+q))例如:((x_1 q+x_2 p)/(p+q),(y_1 q+y_2 p)/(p+q))
After some time I found that the SendKeys-statement works as advertised only and ONLY if there's no further statement in my macro.一段时间后,我发现 SendKeys 语句仅在我的宏中没有进一步的语句时才像宣传的那样工作。
I know this is a bit awkward, but there it is.我知道这有点尴尬,但就是这样。
Consider the following:考虑以下:

Public Sub test()

    Dim test As String
    test = ActiveWindow.Selection.Text

    SendKeys "%=", True

'    MsgBox test

End Sub

This works fine.这工作正常。
Copy/paste the formula above in a Word-document, open VBA in Word, make a new Module and copy/paste the Sub above into it.将上面的公式复制/粘贴到 Word 文档中,在 Word 中打开 VBA,创建一个新模块并将上面的 Sub 复制/粘贴到其中。
Select the formula.选择公式。
You must start the Sub form the Macro's menu (Alt+F8).您必须从宏的菜单 (Alt+F8) 中启动子窗体。
You'll notice that the formula will change in an equation-field and gets centered in the middle.您会注意到公式将在方程字段中更改并居中。
Run the macro again and the formula is changed back in standard text-format.再次运行宏,公式将改回标准文本格式。 Now activate the MsgBox-statement and you'll notice that the SendKeys-statement doesn't work anymore.现在激活 MsgBox 语句,您会注意到 SendKeys 语句不再起作用。
I know this is hardly a workaround for the problem, but maybe someone will find this information useful!我知道这几乎不是解决问题的方法,但也许有人会发现这些信息很有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM