Using AppActivate and Sendkeys in VBA shell command - shell
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 then process B. It works fine for the first iteration. When I use AppActivate to go back to process B, it actually does switch focus back to process B. HOWEVER, it ignores subsequent commands from SendKeys.
Example code:
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
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.
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
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
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 :)
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
You forgot to add "DoEvents" between each sendkeys and kind of delay/wait period, to give it some time to execute.
Related
There is another way do Copy and Paste in VBS or BAT?
I was trying to automatize some repeated fill forms that I do in my job. I automate a lot of in VBA Excel, and I wanted to do with VBS too, but the company's computer doesn't work "^{C}", eather "%{TAB}". In resume, ALT + or CTRL+ doesn't has the common response and a lot of functions are blocked... There is another way to Change Window and Copy/Paste?
I found the problem: .vbs has a lot of bugs in the blocked computer. Ctrl + C or V works better as lower case: "^{c}" & "^{v}" And the % + something doesn't work in any way. So worked like this way for me: Dim WS set WS = CreateObject("WScript.Shell") WS.AppActivate "Protocolos - Internet Explorer" ' name of the sheet CScript.Sleep 100 WS.SendKeys "^{c}" CScript.Quit
SEND clip as keystrokes
I am just wondering if there is any way in manner of PSH or VBS to paste/SEND the text from clipboard as separate keystrokes rather then string???? It is VERY important, since I have to do some automatic search in web UI drop down lists and passing text by CTRL+V simply does not work. So I need to send each letter as unique keystrokes. Tried to find some way and searched over the Internet but no luck. Thank you in advance
There are a few ways to interact with the clipboard in VBScript. Here's one way to read clipboard text: strText = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text") Once you have the text, you could simply send each letter in sequence: For i = 1 To Len(strText) SendKeys Mid(strText, i, 1) Next
Send windows key in vbs?
I've looked everywhere for this, is it actually possible to do? If you press WIN + LEFT ARROW it will mount your selected window to the left of your screen, and that's exactly what I'm trying to do. I have tried things like: shl.sendkeys "#{LEFT}" Because I found out that # could be for WIN just like + is for Shift and % is for Alt. This may be a stupid question but it would help me out so much. Thank you for you time
Set Keys = CreateObject("WScript.Shell") Keys.SendKeys("^{Esc}") This should work, it Simulates the push of the Windows key by pushing the CTRL ^ and the ESC {Esc}keys, i don't know how to make the window mount to the left of the screen
VBScript's SendKeys doesn't support the Windows key. You can use AutoIt instead: Send("{#Left}")
Oh, so you want to mount the window to the left, I currently don't think there's a way to do that, but there's another shortcut for start menu, as well as windows key, it's Ctrl+Esc, ^{ESC}, you could possibly run the onscreen keyboard, then somehow tab over to the key, but it would be hard, also how about trying Alt+Space, %{SPACE}, to click the window icon then, M for move, then set the cursors position to the left of the screen, somehow? I don't currently need help moving the cursor in V.B.S., but you could Google it. Hash symbol is actually typing the symbol #, hash, tested with this. I also got help on the send-keys function, here. I have way to much researching, but I still can't find a answer. Some-one also said using "Auto hotkey", a third party program, but I don't really trust programs that can take control over your whole PC. So I guess your options are: Alt+Space to click window icon, then M for move, then move cursor to left side of the screen Auto HotKey, although I don't recommend it. Other commands, but SendKeys, in VBS, Running a file, built in a different language to do so. Or well, waiting-for / asking Microsoft to add it in, in the next version of Windows?!
a very late response, but here is my working effort Dim shellObject : Set shellObject = CreateObject("WScript.Shell") Extern.Declare micVoid, "keybd_event", "user32.dll", "keybd_event", micByte, micByte, micLong, micLong Const KEYEVENTF_KEYUP = &H2 Const VK_LWIN = &H5B Wait 5 Call Extern.keybd_event(VK_LWIN,0,0,0) 'press win key Wait 5 shellObject.SendKeys "({UP})" 'right aline the window. TODO Error handlng Wait 5 Call Extern.keybd_event(VK_LWIN,0,KEYEVENTF_KEYUP,0) 'up win key Set shellObject = Nothing
try this wshshell.sendkeys "{lwin}"
vb6: interrupt endless msgbox loop
I am writing a program in VB6. By mistake many times my code contains an endless loop, inside which there is a message box. For example: while a>0 msgbox "a is positive" wend Then I press the play/run and I realize what has happened. Is there any way to stop the debugging/running of my program? The only thing that works so far is Ctrl+Alt+Del and end task. But this way the whole visual basic closes and I lose my unsaved data. (please don't comment that I should save my program more often. I know it (now)). Edit: I found on the internet that maybe esc or ctrl+c or ctrl+break could do the job. The first two do nothing and my laptop doesn't have a break key Solution: I have the insert key in my laptop. on the key there is also written pause for use along with the Fn key. So I was able to break the application by pressing Ctrl+Fn+Insert (which maybe could be translated in Ctrl+Pause) edit: link to photo of my keyboard:
ctrl + break will work. If you don't have those keys, use the on screen keyboard. Start | Run | osk Then press ctrl + break.
DOS batch file - Display popup message with two choices
Using a DOS batch file I'd like to display a popup window in XP and Windows 7 where I display a message and give the user the choice to press one of two buttons - one would stop the DOS batch file, while the other button would allow it to continue. Any hint on what dos batch command/pgm I may use?
The following WSH/VBscript snippet prompts the user to click yes/no, after which you can base logic on the response: 'enter this text into ask.vbs dim input input = MsgBox ("Do you want to continue?", 4) 'wscript.echo input If input = 7 Then 'answer was no End If If input = 6 Then 'answer was yes End If Then in your batch file, call wscript ask.vbs. However, I recommend staying in pure console for this task if you can, as choice and set can handle this nicely as Anders suggests. pause works very well too, and that's usually what I use for a binary choice like yours because it's the simplest approach.
To display an actual window you would have to call something external, a .exe or a Windows Scripting Host script. If you want to stay in pure batch, echo the choices and use SET /P to wait for input from the user. (To support DOS and Win9x you would have to use choice and not set on those systems)