Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced VBA Question

Status
Not open for further replies.

monsjic

Programmer
Jan 23, 2002
23
US
Does anyone know if the following is possible in VBA?

I know you can launch files from the command prompt. For example, I could open a Word file from the command prompt.

But is there a way to programatically send keys to the program??? For example, I could send Ctrl-A and Ctrl-C in VBA and then capture all of the text of the document, and use this in a query or something else.

Thanks for your help
 
I think you would definitely want to use an Automation Object in this case. You can use OLE View to check out the interfaces for these objects. Word is a pretty easy one. I recommend you dimension the object as it's class rather than as a generic object and then use SET to instantiate it. That will enhance performance because most of the work is done at compile time. In your code module select tools|References find and add Microsoft Word Object Library.
Then in your functions/subs
Dim WordApp as Word.Application
Dim MyDocuments As Word.Documents
Dim MyDocument as Word.document

Set WordApp = New Word.Application
Set MyDocuments = Word.Application.Documents
Set MyDocument = MyDocuments.Open("thepathtoyourdocument")

then you can copy the contents or use other cool document methods.

Make sure you set all these objects = nothing when you're done.
JHall
 
I agree with jhall156, and once you have the Word.Application up and running, you might send the keys with the SendKeys method. To boldly code, where no programmer has compiled before!
 
I haven't checked it but I'm sure you must be able to, within the document object, 'select all' just like you would from the menu and then likewise copy to the clipboard or assign a variable to the value etc.
JHall
 
I'm a programmer on a new workflow and drm program that has this very feature. They call it screen scraping, pretty cool to see how it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top