problem with Sendkeys

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2005
Posts: 70
Reputation: w00dy is an unknown quantity at this point 
Solved Threads: 2
w00dy w00dy is offline Offline
Junior Poster in Training

problem with Sendkeys

 
0
  #1
Nov 1st, 2005
The object here is to open a selected Word file and Copy&Paste its contents into a text file. The problem seems to be with the Sendkeys commands having no effect. The project freezes, apparently because Notepad has not been closed (by Sendkeys "%FX").

Can anyone tell me what's wrong?
Attached Files
File Type: zip NCrypt.zip (2.4 KB, 32 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: problem with Sendkeys

 
0
  #2
Nov 1st, 2005
Nice Code Here, But I would Personally Choose a different style. I think you have the right idea by using the word object, but I think you should use the methods of the word object to completely bypass the use of sendkeys (Well, At Least Put To A Minimum). For Example: http://www.dx21.com/SCRIPTING/VBSCRIPT/EXAMPLE.ASP, and then click the eye for "Spell Check Clipboard with Microsoft Word", it shows how to copy and paste information from Word Using The Word Object, However, That Wasn't Your Question, Just My Suggestion.

To Answer Your Question, I see two Major problems. One Is That Word Is Never Shown, or Set To Visible. This is a problem, because I'm pretty sure that sendkeys only works with active AND Visible Windows. The Second Issue, Is That VB Would be sending Keys (The File Open And All That) WITHOUT WAITING For Word To Be Launched. When You Instantiate A New Instance Of Word, It might take a variable amount of time (depending on the RAM and Processor Of The Machine in question). I suggest A couple of fun API Calls, One Is "FindWindow" (Or FindWindowEx If you want to be more daring), and "setforegroundwindow", however, you could also use AppActivate. In Order to get word to show (after creating the word object), you'll need to set it's .visible property (or invoke it's .show method, depends on the version you are using).

Sorry to be long winded, but I hope that some of the suggestions will help you out.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 70
Reputation: w00dy is an unknown quantity at this point 
Solved Threads: 2
w00dy w00dy is offline Offline
Junior Poster in Training

Re: problem with Sendkeys

 
0
  #3
Nov 2nd, 2005
Hi Comatose, thanks for the very interesting reply, and the compliment, but I must confess much of the code wasn't mine - I was going to use Shell, but then found an example using API's , and combined it with the code I already had. I'm new to this area of VB programming, and floundering a bit. I will follow up your advice, and post back when it's working.
w00dy
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: problem with Sendkeys

 
0
  #4
Nov 2nd, 2005
Alright...

Read Over This Code (It's Fully Commented) and you can pick out what you need, so that you can add it to your project and have it work the way you want it to. Right now, it (the wordnotepad) copies all the text in a word document (in this program, the path is the same as the running project [meaning the word document should be in the same folder], and it's name is test.doc), launches notepad, waits until it see's that notepad is loaded, and then sends it a paste command using Sendkeys.

This Method, however, is still not ideal. The reason, is because there are a lot of restrictions in place for it. You have to make sure that notepad is visible, and that it is the active (foreground) window. This also doesn't take into account any popup windows, or programs that might jump to the front of all the applications because of some event. Granted, (on my PC at least) once the VB app sees the notepad window, the sendkeys is almost immediate (before the form loads, in fact), but keep in mind that the possiblity for being interrupted and throwing the whole system out of whack does exist with the sendkeys method. Nevertheless, I have attached a project that shows how that can be done.

The Best Alternative That I have Found thus far, is to create a word instance, and use the "saveas" method to save the word document as a .txt file (you can generate a random name, or whatever), and then spawn notepad (passing the path and file name of the newly saved textfile). This avoids the use of an API Call, AND avoids the possibility of outside interference of the sendkeys. I've attached Both, so that you can decide which one you want to include in your own code (if any). Let me know what you decide to do.
Attached Files
File Type: zip WordNotepad.zip (3.8 KB, 58 views)
File Type: zip WordFile.zip (3.3 KB, 34 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 70
Reputation: w00dy is an unknown quantity at this point 
Solved Threads: 2
w00dy w00dy is offline Offline
Junior Poster in Training

Re: problem with Sendkeys

 
0
  #5
Nov 3rd, 2005
Thanks for those, Comatose, believe it or not I had solved the problem (almost) before I read your last post with this:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2. Dim WordApp As Word.Application
  3. Dim WordDoc As Word.Document
  4.  
  5. Private Sub cmdSelect_Click()
  6. Set WordApp = CreateObject("Word.Application")
  7. WordApp.Visible = False
  8.  
  9. ComDlg1.ShowOpen
  10. Set WordDoc = WordApp.Documents.Open(ComDlg1.FileName)
  11. WordApp.Selection.WholeStory
  12. WordApp.Selection.Copy
  13.  
  14. WordApp.ActiveDocument.Close
  15. WordApp.Quit
  16. Set WordDoc = Nothing
  17. Set WordApp = Nothing
  18.  
  19. Open "C:\Program Files\NCrypt\Ntemp.txt" For Output As #1
  20. Write #1, Clipboard.GetText
  21. Close #1
  22. End Sub

However, in my effort quotes are pasted to the start and end of the text file, which I didn't want, but your example doesn't have that problem. Thanks again,
w00dy
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: problem with Sendkeys

 
0
  #6
Nov 3rd, 2005
No Problems, I know you'll help me when I need it! You could leave your code the same as what you posted above, only change the write #1 command to a print #1, and you'll remove the quotes .
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 4
Reputation: Gweneith is an unknown quantity at this point 
Solved Threads: 1
Gweneith Gweneith is offline Offline
Newbie Poster

Re: problem with Sendkeys

 
0
  #7
Nov 17th, 2005
Hi Comatose and Woody... Is it possible that i will open a selected excel file where it has some contents that i need to copy and paste to an application( like an accounting software). The objective here is to minimize repetitive task of inputting same details for each customers. Does SendKey Command works on it? Any suggestions on how to implement such delimma. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: problem with Sendkeys

 
0
  #8
Nov 17th, 2005
Certainly, however there are some things to consider. If the program in discussion is run from a DOS prompt (meaning, it's a DOS based program, so, it shouldn't really have a "windows" window.), then you'll have to get creative. DOS Prompts, in my experience don't work well with sendkeys, so you would need to copy the data to the clipboard, and then send an "alt space ep" ("% ep", which is alt, space, e [for edit] and p [for paste]) to paste the data from the clipboard to the DOS prompt or DOS Based Program.

If you are working in a windows based program, for example, transfering data from excel to quicken or something crazy, you could absolutely create an instance of the excel object, load the spreadsheet of your choice, read in the data from the cells, and transfer that information to another windows based program no problem. Let me know if you need any further assistance in this matter.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 4
Reputation: Gweneith is an unknown quantity at this point 
Solved Threads: 1
Gweneith Gweneith is offline Offline
Newbie Poster

Re: problem with Sendkeys

 
0
  #9
Nov 18th, 2005
Originally Posted by Comatose
If you are working in a windows based program, for example, transfering data from excel to quicken or something crazy, you could absolutely create an instance of the excel object, load the spreadsheet of your choice, read in the data from the cells, and transfer that information to another windows based program no problem. Let me know if you need any further assistance in this matter.
It's actually a windows application. Hope u can assist me in this matter. Do u have any sample codes that cater such requirements?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: problem with Sendkeys

 
0
  #10
Nov 18th, 2005
Well, I don't know the application's setup on how it would need the data to be sent to it.... I can certainly give you some code however, to open up and read excel files into VB. Actually sending that data to the other program with sendkeys is a meticulous task. For example, if, on the keyboard, you would press TAB to go to the next field in your software, then you would have to have VB send the TAB, and then the paste, and to move to the next field another TAB. Basically, with sendkeys, you are telling VB to pretend that it is sitting at the keyboard, sending keystrokes to the other application (typing a heck of a lot faster than any of us :eek: ). You have to take into consideration every step that you would normally do yourself, and make the VB Program handle all of those steps.

If You look at the WordNotepad.zip file attached above, you'll see the sendkeys line does only 1 thing. It sends just a ctrl-v (^v) which is the windows keyboard shortcut for paste. In your program, however, it will be much much more complex. If you are just pasting data to notepad, it's no big deal, but to do step by step what you need to (what you want to do) requires intimate knowledge of the software you are sending the data to. Does that make sense?
Last edited by Comatose; Nov 20th, 2005 at 10:13 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC