elb*_*laf 6 shell vba sendkeys
我想在VBA中使用shell命令在应用程序之间来回切换.我正在使用SendKeys来处理进程A中的内容然后转移到进程B,然后进行处理A然后进程B.它适用于第一次迭代.当我使用AppActivate返回进程B时,它实际上确实将焦点切换回进程B.但是,它忽略了来自SendKeys的后续命令.
示例代码:
Sub pastePDF2TXT_v3(pdfName As String, txtName As String)
Dim acrobatID
Dim acrobatInvokeCmd As String
Dim acrobatLocation As String
Dim notepadID
Dim acrobatID2
Dim notepadID2
Debug.Print "here"
acrobatLocation = "C:\Program Files\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
acrobatInvokeCmd = acrobatLocation & " " & pdfName
acrobatID = Shell(acrobatInvokeCmd, 1)
AppActivate acrobatID
SendKeys "^a", True '^A selects everything already in the pdf file.
SendKeys "^c", True '^C copies the selection to the clipboard.
notepadID = Shell("NOTEPAD.EXE " & txtName, 1) ' invoke notepad on the text file.
AppActivate notepadID ' set the new app as teh active task.
SendKeys "^a", True '^A selects everything already in the text file.
SendKeys "^v", True '^V pastes the new stuff over the top of the old text file (deleting the old stuff)
SendKeys "%{END}", True ' makre sure last line of text file
SendKeys "{ENTER}", True
AppActivate acrobatID ' NOTE: APPEARS TO WORK UP TO THIS POINT.
SendKeys "{ENTER}", True ' NOTE: SECOND APP IGNORES SUBSEQUENT COMMANDS FROM HERE DOWN.
SendKeys "^a", True '^A selects everything already in the pdf file.
SendKeys "^c", True '^C copies the selection to the clipboard.
SendKeys "%f", True 'alt f, x to exit Notepad.exe
SendKeys "x", True
'acrobatID.Quit
Debug.Print "start second"
AppActivate notepadID ' set the new app as teh active task.
SendKeys "%{END}", True 'Go to end of text file.
SendKeys "^v", True '^V pastes the new stuff at end of file.
SendKeys "{ENTER}", True
SendKeys "^s", True
SendKeys "%f", True 'alt f, x to exit Notepad.exe
SendKeys "x", True
notepadID.Quit
acrobatID.Quit
End Sub
Run Code Online (Sandbox Code Playgroud)
这可能更像是评论而不是答案,但我似乎不被允许“评论”,所以......
令人惊奇的是你已经取得了如此成就。很有可能,你永远无法可靠地完成这项工作——就这样。一旦你让它工作起来,在未来的版本中,Acrobat UI 的行为方式就会发生一些变化,或者 Windows 将会对哪些东西可以向其他东西发送事件的规则进行另一次更改,然后你再次陷入困境。
在这种情况下,您可能遇到的情况是,当用户明显忙于与第一个应用程序交互时,Windows 试图阻止某个应用程序从另一个应用程序中窃取焦点。在尝试执行诸如在一个应用程序中使用按钮将数据写入临时文件并打开 MS Word 进行编辑之类的操作时,您会看到同样类型的问题。Windows 会阻止焦点从当前应用程序转移到 MS Word,因为您刚刚单击了当前应用程序中的按钮。
因此,我们不要试图解决这个不可能的技术问题,而是退后一步,问一下您最初希望通过执行所有这些操作来实现什么目标。通过这种方式,也许我们可以带您到达您想去的地方:)