vba - VBA SendKeys到另一个窗口不起作用
问题描述
我有以下宏,旨在快速切换两个 PDF。8 次快速切换后,宏应转到两个 PDF 的下一页并重复该过程。不幸的是,宏滚动最初只声明了 PDF。任何想法如何修改它?
Private Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal lngHWnd As LongPtr) As LongPtr
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMiliseconds As Long)
Sub switching_pdfs()
Dim i As Integer, j As Integer
ptr1 = FindWindow(vbNullString, "Some PDF 1.pdf - Acrobat Reader")
ptr2 = FindWindow(vbNullString, "Some PDF 2.pdf - Acrobat Reader")
For i = 1 To 30
For j = 1 To 4
BringWindowToTop (ptr1)
Sleep 100
BringWindowToTop (ptr2)
Sleep 100
Next j
BringWindowToTop (ptr1)
Application.SendKeys "{RIGHT}": Sleep 500: DoEvents 'should move to the next page in the first PDF
BringWindowToTop (ptr2)
Application.SendKeys "{RIGHT}": Sleep 500: DoEvents 'should move to the next page in the second PDF
Next i
End Sub
我也尝试使用 SendMessage,但它不想将任何 PDF 移动到下一页。
解决方案
试试这个方法。测试和工作。您可以更改等待数字以延长延迟时间。确保 PDF 文件命名正确并且文件名上没有多余的空格。
Sub switching_pdfs()
Dim i As Integer, j As Integer
Dim ptr1 As String, ptr2 As String
ptr1 = "Some PDF 1.pdf - Acrobat Reader"
ptr2 = "Some PDF 2.pdf - Acrobat Reader"
For i = 1 To 30
For j = 1 To 4
AppActivate ptr1
Wait 0.5
AppActivate ptr2
Wait 0.5
Next j
AppActivate ptr1
Send "{RIGHT}"
Wait 1
AppActivate ptr2
Send "{RIGHT}"
Wait 1
Next i
End Sub
Function Send(pData As String)
SendKeys pData, True
Wait 0.5
End Function
Function Wait(Optional pWaitTime As Single = 0.1)
Dim StartTime
StartTime = Timer
Do While (Timer < StartTime + pWaitTime)
DoEvents
Loop
End Function
推荐阅读
- javascript - 我的引导程序在我的 js 文件中无法正常工作
- python - 随机 numpy 数组,其值介于 -1 和 1 之间,总和为 1
- json - 空手道框架:打印 JSON 时缺少双引号
- r - 在 R 中使用 projectRaster 后,栅格范围不再正确
- plsql - 通过动态操作对页面项目进行验证
- javascript - 闪亮和数字格式中的条件文本样式
- javascript - 使用无效数据调用 Firestore 函数 DocumentReference.update()。不支持的字段值:自定义对象
- microsoft-graph-api - Microsoft Graph 邮件查询 - 获取“ErrorAccessDenied”
- swift - 我应该使用什么方法来代替已弃用的“resumeAnimation(forKey:)”?
- python - 锁定鼠标后,有什么方法可以让程序打开视频吗?