sendkeys_Excel中SendKeys的问题

sendkeys

Yes, I know that it’s a bad idea to use the SendKeys method in Excel, because strange things can happen.

是的,我知道在Excel中使用SendKeys方法是一个坏主意,因为可能会发生奇怪的事情。

However, it’s handy in a few situations, and I use SendKeys in a few of my Comments macros.

但是,在某些情况下它很方便,并且在一些Comments宏中使用了SendKeys。

For example, in this macro to insert a blank comment, without a user name, the comment opens for editing, at the end of the macro.

例如,在此宏中插入没有用户名的空白注释时,注释将在宏末尾打开以进行编辑。

Sub CommentAddOrEdit()
  Dim cmt As Comment
  Set cmt = ActiveCell.Comment
  If cmt Is Nothing Then
    ActiveCell.AddComment text:=""
  End If
  SendKeys "+{F2}"
End Sub

发送键盘快捷键 (Send Keyboard Shortcuts)

In that example, the SendKeys line simulates using the keyboard shortcut – Shift + F2 – to edit the comment in the active cell.

在该示例中,SendKeys行使用键盘快捷键Shift + F2进行模拟,以在活动单元格中编辑注释。

editcomment01

SendKeys无法运行 (SendKeys Doesn’t Run)

While I was updating the Comments VBA page, I wanted to test a few of the macros, to make sure that they still worked in Excel 2010. To make it easier to run a macro, I added a keyboard shortcut for it – Ctrl + Shift + C.

在更新“注释” VBA页面时,我想测试一些宏,以确保它们仍可在Excel 2010中使用。为了更轻松地运行宏,我为其添加了键盘快捷键-Ctrl + Shift +C。

macrorun02

When I tested the macro, using that shortcut, it inserted the comment, but the comment didn’t open for editing. Hmmm…maybe that shortcut code doesn’t work in Excel 2010.

当我测试宏时,使用该快捷方式插入了注释,但注释未打开进行编辑。 嗯……也许快捷方式代码在Excel 2010中不起作用。

But, when I ran the macro from the Macro window, instead of the shortcut, it worked correctly. So, the problem wasn’t the SendKeys code. There was something funny happening with the shortcut to run the macro.

但是,当我从“宏”窗口而不是快捷方式运行宏时,它可以正常工作。 因此,问题不在于SendKeys代码。 运行宏的快捷方式发生了一些有趣的事情。

添加等待线 (Add a Wait Line)

Some Googling led me to the Microsoft site, where this problem is in the MSKB: Error Using SendKeys in VB with Shortcut Key Assigned

一些谷歌搜索将我带到Microsoft网站,此问题在MSKB中: 在VB中使用分配了快捷键的错误使用SendKeys

The problem occurs because this is a very short macro, and I was still pressing the Ctrl + Shift keys when the macro runs the SendKeys statement. And that messes up the SendKeys keystrokes. See – I told you that SendKeys was risky!

发生问题是因为这是一个非常短的宏,并且当宏运行SendKeys语句时,我仍然按Ctrl + Shift键。 这搞砸了SendKeys击键。 看–我告诉你SendKeys有风险!

The suggested solution is to add a Wait line in the macro, just before the SendKeys code. So, I altered the macro, and now it runs correctly when I use the keyboard shortcut.

建议的解决方案是在宏中的SendKeys代码之前添加Wait行。 因此,我更改了宏,现在当我使用键盘快捷键时它可以正确运行。

Sub CommentAddOrEdit()
  Dim cmt As Comment
  Set cmt = ActiveCell.Comment
  If cmt Is Nothing Then
    ActiveCell.AddComment Text:=""
  End If
    Application.Wait (Now() + TimeValue("00:00:01"))
    SendKeys "+{F2}"
End Sub

翻译自: https://contexturesblog.com/archives/2013/01/29/problems-with-sendkeys-in-excel/

sendkeys

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值