简体   繁体   中英

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 . 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 to get the same result reliably although more advanced knowledge of VBA/programming is required when using Win API

I'm also having trouble with the SendKeys-statement.
I'm trying to send Alt= to a Word document. 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))
After some time I found that the SendKeys-statement works as advertised only and ONLY if there's no further statement in my macro.
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.
Select the formula.
You must start the Sub form the Macro's menu (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.
I know this is hardly a workaround for the problem, but maybe someone will find this information useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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