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?
:confused:

Recommended Answers

All 12 Replies

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.

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

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.

Thanks for those, Comatose, believe it or not I had solved the problem (almost) before I read your last post with this:

Option Explicit
Dim WordApp As Word.Application
Dim WordDoc As Word.Document

Private Sub cmdSelect_Click()
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = False

ComDlg1.ShowOpen
Set WordDoc = WordApp.Documents.Open(ComDlg1.FileName)
WordApp.Selection.WholeStory
WordApp.Selection.Copy

WordApp.ActiveDocument.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing

Open "C:\Program Files\NCrypt\Ntemp.txt" For Output As #1
Write #1, Clipboard.GetText
Close #1
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 :)

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 :).

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!

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.

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?

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?

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?

Hey sorry for the late reply... yeah i guess you're right. Maybe for now, I'll focus on simple steps first..and later i'll try to explore to more complex use of send key... I did look at the codes for the WordNotepad, I'll try to come up with an idea from there. Thanks a lot for your help. If you have any codes that you think it can help me more...i will appreciate it very much.

Hey Comatose..im kinda interested and i have some running idea... You mentioned about opening up and read excel files into VB. Can u share with me your code for this application? Thanks a lot.

First thing is first, you have to declare an object variable for the excel object, and then you have to instatiate an instance of the excel object. You should also consider creating an object to the workbook in the excel document. It would start off looking something like this:

dim objExcel
dim objWorkbook

Set objExcel = CreateObject("Excel.Application")
' /* obviously, you need to change "c:\path\yourfile.xls" to your excel document */
Set objWorkbook = objExcel.Workbooks.Open("c:\path\yourfile.xls")

Then, you can choose which Sheet (ya know, the tabs at the bottom) you want to work with, with a call to Select, like this:

objExcel.ActiveWorkbook.Sheets(1).Select()

Then, you can read and write to cells in the document, by referencing them by row and column, so, you could set a variable to the data in row 2, column 1 like this:

CellValue =  objExcel.Cells(2, 1).Value

And similarly, you can write to the excel document in just the opposite way, like this:

objExcel.Cells(2, 1).Value = "hello"

And no matter what, when you are done using the excel document, it's a very important point to make sure that you set the object references to nothing, like so:

set objExcel = nothing
set objWorkbook = nothing

Here is a pretty good site to help you through understanding how you can work with excel in VB, reading data, and writing data: http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=315, if you need any further assistance with this, let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.