There is another way do Copy and Paste in VBS or BAT? - vbscript

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

Related

Code that will allow me to Left mouse click, I can only use Notepad to write the code and java is installed

I'm trying to write a script in VBScript that executes a series of events and one of them includes a left mouse click and release.
I know that I can't use VBScript in order to do the left mouse click and I can't install any software. I am only able to use Notepad in order to do all the code writing. I do have Microsoft word as well but I don't know if you can use that to write any code.
Is there a way for me to write some simple code in another language that allows me to left mouse click and release? I'm then planning on importing this code into my VBScript and continue on from there.
What I have so far:
Option Explicit
Dim Excel, x, y
Dim s
Set Excel = WScript.CreateObject("Excel.Application")
x = "10"
y = "10"
Excel.ExecuteExcel4Macro ( _
"CALL(""user32"",""SetCursorPos"",""JJJ""," & x & "," & y & ")")
WScript.Sleep (1000)
WScript.Sleep (1000)
WScript.Echo "Stop Script, Test test"
Thanks for reading!

How to Move the mouse using VBScript

I am trying to move the mouse using VBScript. I tried to use Sendkeys "{CLICK LEFT , x , y}" and Sendkeys "{MOVETO, 10 , 20}" but it isn't working i also tried to use MouseKeys, so i could move it with the keyboard and therefore use Sendkeys to activate it but it also did not work (the mousekeys numpad is not moving the mouse). I've tried everything I know and what i could research elsewhere, so now I hope one of you can answer this for me. thxs
One possible way to move the mouse is:
Dim Excel: Set Excel = WScript.CreateObject("Excel.Application")
Excel.ExecuteExcel4Macro "CALL(""user32"",""SetCursorPos"",""JJJ"",""xxx"",""yyy"")"
-->xxx = X Position
| yyy = y Position
The only disadvantage is that you need Microsoft Excel to run this script...
Hope I could help you
VBScript can't do this natively. You'd have to do some calls to the Windows API or some other library that can do this for you.
As an alternative, you may want to consider a different scripting language, like AutoHotKey which can do this in one simple line of code for you.

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}"

Creating a desktop shortcut out of a key combination?

I need a little desktop shortcut that I could click with a mouse. It needs to perform the action WINDOWS+D.
I was told that I could build a .VBS file to create a desktop shortcut but I have absolutely no idea how to do so. I'm clueless about coding and this sort of tech stuff.
All I could find was this:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^+{ESCAPE}"
Theoretically I could just replace those values for WINDOWS+D, but I don't know their correspondent strings.
So, how can I accomplish that?
Thanks!
Instead of that, why not try:
Set shell = wscript.CreateObject("Shell.Application")
Shell.MinimizeAll
Set Shell = nothing
Or maybe:
set Shell = wscript.CreateObject("Shell.Application")
Shell.ToggleDesktop
Set Shell = nothing
I don't have a Win 8 machine to test with right now. Let me know if it works.

Using AppActivate and Sendkeys in VBA shell command

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.

Resources