相关疑难解决方法(0)

Excel VBA:通过快捷键运行时打开文档后宏挂起,但在 VB 编辑器中完美运行

我遇到了一个奇怪的问题,我决定Ctrl + Shift + P在 VBA 中为我的一个例程分配一个键盘快捷键。此例程假设打开现有的 excel 工作簿,复制一些信息和.SaveAs另一个名称。

当我在 Visual Basic 编辑器中点击播放时,这实际上运行良好。但是,当我使用 Excel 中的键盘快捷键运行我的代码时,它在打开文档后停止工作。

我在此示例中重现了该问题:

Public Sub WriteData()
    Dim templatePath As String
    Dim templateWorkbook As Workbook

    'it seems to hang on the following line
    Set templateWorkbook = Application.Workbooks.Open(templatePath)

    With templateWorkbook.Worksheets.Application
        .Range("B3") = "Employee name"
        .Range("B4") = "Employee first name"
        .Range("B5") = "Employee email"
    End With

    templateWorkbook.SaveAs(ThisWorkbook.Path & "Test")
    templateWorkbook.close
End Sub
Run Code Online (Sandbox Code Playgroud)

我正在使用 Excel 2007。

excel vba keyboard-shortcuts

3
推荐指数
1
解决办法
6479
查看次数

在vba powerpoint中按键时调用Sub

此代码捕获活动窗口并将其粘贴到功率点幻灯片上,方法是通过调用a来粘贴数字屏幕截图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
Run Code Online (Sandbox Code Playgroud)

这是我的打印屏幕子.

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, …
Run Code Online (Sandbox Code Playgroud)

vba keyboard-shortcuts powerpoint-vba

2
推荐指数
1
解决办法
7886
查看次数

标签 统计

keyboard-shortcuts ×2

vba ×2

excel ×1

powerpoint-vba ×1