当我在 vba powerpoint 中按下一个键时调用一个 Sub

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27523054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:33:10  来源:igfitidea点击:

Invoke a Sub when I press a key in vba powerpoint

vbakeyboard-shortcutspowerpoint-vba

提问by Ali Malik

This code captures the active window and paste it to power point slides by taking number screenshots to be pasted, by calling a sub PrintScreen, After that it takes 5 seconds to capture screen of active window. While i want that whenever i press a specific key like 'F7 or F3 etc' it should take print screen instead of waiting 5 seconds. I just want to press the key and it invokes sub in which i specify the print and paste and other functions.

此代码通过调用要粘贴的数字屏幕截图来捕获活动窗口并将其粘贴到幻灯片中sub PrintScreen,然后需要 5 秒来捕获活动窗口的屏幕。虽然我希望每当我按下“F7 或 F3 等”等特定键时,它应该打印屏幕而不是等待 5 秒。我只想按下该键,它会调用 sub,我在其中指定了打印和粘贴以及其他功能。

Sub Screen_Capture_VBA()
 Dim Sec4 As Date
 MsgBox "Note: Three seconds after you click OK " & _
 "the active window will be copied to the clipboard."
 Sec4 = DateAdd("s", 4, Now)
 myValue = InputBox("Give me no of screen shots you want to capture")
 For I = 1 To myValue
 PrintScreen
 Next I
End Sub

This is my print screen sub.

这是我的打印屏幕子。

Sub PrintScreen()

Sleep 5000
keybd_event VK_MENU, 0, 0, 0
keybd_event VK_SNAPSHOT, 0, 0, 0
keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0

ActivePresentation.Slides.Add 1, ppLayoutBlank
ActivePresentation.Slides(1).Shapes.Paste

End Sub

回答by Andi Mohr

PowerPoint doesn't support assigning keyboard shortcuts to macros like in Excel. You can purchase third-party apps like OfficeOne Shortcut Managerbut a simpler workaround is to add your macro to the Quick Access Toolbar.

PowerPoint 不支持像在 Excel 中那样为宏分配键盘快捷键。您可以购买第三方应用程序,如OfficeOne 快捷方式管理器,但更简单的解决方法是将您的宏添加到快速访问工具栏。

  1. Click the QAT dropdown arrow (highlighted yellow below)
  2. Click More Commands
  3. In the dropdown Choose commands fromselect Macros
  4. Click the YourPresentationName!PrintScreenmacro
  5. Click the Add >>button
  6. Click OK
  1. 单击 QAT 下拉箭头(下面以黄色突出显示)
  2. 点击 More Commands
  3. 在下拉列表中Choose commands from选择
  4. 单击YourPresentationName!PrintScreen
  5. 点击Add >>按钮
  6. 点击 OK

enter image description here

enter image description here

Then you can use alt+1(or whatever number the QAT assigns) to execute PrintScreen(). Press the altkey once to see what number shortcut PowerPoint has assigned to your macro.

然后您可以使用alt+ 1(或任何 QAT 分配的数字)来执行PrintScreen()。按alt一次该键以查看 PowerPoint 已分配给您的宏的编号。

enter image description here

enter image description here

回答by Ajmal Jamil

Due to lack of features powerpoint doesnot support onkey. So try to write a code in C++and assign keys to it and shell execute your code, like in your case assign F7to ALT+PrintScrni.e when you press F7 it should take screenshot of your current window. Here is the code for shell execution

由于缺乏功能,powerpoint 不支持 onkey。因此,尝试用C++编写代码并为其分配键,然后 shell 执行您的代码,就像在您的情况下将F7分配给ALT+PrintScrn即当您按 F7 时,它应该截取当前窗口的屏幕截图。这是shell执行的代码

Sub PrintScreen()
Dim sFullPathToExecutable As String
sFullPathToExecutable = "C:\Users\abc.exe"
Shell sFullPathToExecutable
End Sub