Using AppActivate and Sendkeys in VBA shell command

后端 未结 6 971
一个人的身影
一个人的身影 2021-01-05 16:30

I want to switch back and forth between application with the shell command in VBA. I am using SendKeys to do stuff in process A then move to process B, then to process A the

相关标签:
6条回答
  • 2021-01-05 16:46

    Similarly, I was trying to use AppActivate on a pdf document I had opened with a shell command so that I could use SendKeys on it. It would always generate Run Time Error '5'. My solution, eventually was to NOT use AppActivate at all, in fact opening the document brings it to the forefront anyway. The problem is that the SendKeys statement executes immediately after the shell command but the pdf needs a second or two to open up. My solution is to pause the code for a couple seconds before excuting the sendkeys statement.

    I used a time delay curtosy of pootle flump. Check out the thread here: http://www.dbforums.com/microsoft-access/1219379-time-delay-vba.html

    0 讨论(0)
  • 2021-01-05 16:50

    You forgot to add "DoEvents" between each sendkeys and kind of delay/wait period, to give it some time to execute.

    0 讨论(0)
  • 2021-01-05 16:51

    I know this question is old, but I have run into this same problem, but I don't think the answer that was chosen is the correct one.

    When you call AppActivate, the script goes on halt, waiting for the target application to terminate. After the application is terminated, the script continues. I see no solution to this issue.

    EDIT:

    I use a batch-file to call CSCRIPT for the AppActivate command, and I notice you are strictly using a VBS file. Try calling CMD as a new window and pass CSCRIPT //NoLogo //B WScript.CreateObject("WSCript.shell").AppActivate("Window Name") as the argument, and see if that bypasses the issue.

    0 讨论(0)
  • 2021-01-05 16:51

    After hours of research on various forums, I could figure out that I wont be able to use Adobe library objects and functions with Adobe Reader. The only viable option left was to use Shell Commands to use "save as text" option available in the Adobe Reader's file menu. The shortcut key is Alt+f+a+x+s. I implemented these in the below code which worked perfectly, although I was required to insert delays in some steps.

    Sub SavePDFasText()
    Dim AdobeReaderPath As String
    Dim PdfFilePath As String
    Dim PDFid, NotepdId As Double
    
    AdobeReaderPath = "C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"
    PdfFilePath = "D:\PowerGenMacro\opm_11.pdf"
    PDFid = Shell(AdobeReaderPath & " " & PdfFilePath, vbNormalFocus)
    Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 5)
    'Alt+f+a+x is required to save the pdf as text in the adobe reader
    SendKeys "%(FAX)", True
    Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 2)
    SendKeys "%S", True
    SendKeys "^q", True
    
    End Sub
    
    0 讨论(0)
  • 2021-01-05 17:07

    This might be more of a comment than an answer, but I don't seem to be allowed to "comment", so...

    It's amazing you've gotten as far as you did. Chances are, you're never going to be able to make this work reliably -- period. As soon as you do make it work, something will change about how the Acrobat UI behaves in a future release, or Windows will make another change to the rules for what things can send events to what other things, and then you're hosed again.

    What you are probably running into in this case is Windows trying to prevent an application stealing the focus from another when a user is apparently busy interacting with the first one. You see the same kinds of problems trying to do things like have a button in one application write data to a temporary file and open MS Word to edit it. Windows prevents the focus from transferring away from the current app and over to MS Word because you just clicked a button in the current app.

    So -- instead of trying to solve this impossible technical issue, let's take a step back and ask what you were originally hoping to accomplish by doing all of this. In that way, perhaps, we can get you where you're trying to go :)

    0 讨论(0)
  • 2021-01-05 17:11

    Try sendkeys "%{tab}" to switch windows but then keep only 2 active windows.

    or try appactivate only after sendkeys {Tab} so that text entry box is not selected before switchibg windows cmd.

    or try try appactivate window name, 1 then sleep 2000 °to allow time to bring that window into focus

    0 讨论(0)
提交回复
热议问题