VBA Code using SendKeys not processing correctly

G

Guest

I am maintaining a db that creates a legal document using SendKeys.

It formats the page in the necessary format using SendKeys to send Alt and
Ctrl sequences.

The first thing the code does, after creating the word doc is to format the
margins and tabs, but the code processes too fast and by the time Word is
ready to move on, several other SendKeys commands have been sent by
VBA/Access.

These keystrokes come in while Word is still 'thinking' and are therefore
ignored.

The routine used to work (pre-Win XP) but now no longer works.

Here is the sample code that is not working right.

Set WordObj = CreateObject("Word.Basic")
WordObj.Filenew
WordObj.AppShow "Microsoft Word"
WordObj.SendKeys "%f", True
WordObj.SendKeys "u.75{TAB}.75{TAB}.5{TAB}.5{ENTER}", True
WordObj.SendKeys "% ", True

by the time this code has run the program is already behind, and future
keystrokes get sent to the tab control form that is still open because the
above keystrokes did not process fast enough.

Now, I have tried inserting a line that counts from 1 to 15,000,000 after
each SendKeys command, and that works on some computers, but some still have
a problem with it.

Any help would be appreciated.

Wanna be
 
G

Guest

Hi,
you should be avoiding using sendkeys. It can cause fatal errors.
I'm sure there is a way to automate word as you want directly through VBA
instead.
HTH
Good luck
 
G

Guest

I am maintaining this db and it is already using SendKeys to create the Word
doc.

If I knew how to use Word to do the task, or how to avoid using SendKeys,
I'd do that.

Perhaps I should create a VBA routine in Word to look in Access and gather
the necessary data, but that is beyond my experience or knowledge.

I guess what I want is something that will work while I recreate the entire
db to make it more useful and efficient.

Thanks for the input freakazeud. Now, I guess I want two things: (1)
something that I can do in a couple of hours that will work for now; and (2)
the best way to create Word docs from Access for the new db that will replace
this dinosaur.

Again, thanks.
 
D

Douglas J Steele

I understand WordObj.SendKeys "%f" as opening the File menu, and
WordObj.SendKeys "u.75{TAB}.75{TAB}.5{TAB}.5{ENTER}" as setting the top and
bottom margins to .75 and the left and right margins to .5, but what's

WordObj.SendKeys "% ", True

supposed to do?

As well, what are you going to do if your user has selected centimeters
rather than inches as their unit of measure?

While it may take a little getting into, Automation is definitely the way to
go. You have the "luxury" of recording a macro in Word that does everything
you want done, and then using that macro as the basis for your automation
routine in Access. http://support.microsoft.com/?id=237337 is an
introduction to the topic. (Note that if you're not using Word 2000, you'll
have to make a slight modification to that KB article. Word 97 will be the
Microsoft Word 8.0 Object Library, Word 2002 will be the the Microsoft Word
10.0 Object Library and Word 2003 will be the Microsoft Word 11.0 Object
Library) While it's dated, I recall that the downloadable help file at
http://support.microsoft.com/?id=167223 was pretty good.
 
G

Guest

Douglas,

You are correct in the keystrokes you have identified.

This line of code
WordObj.SendKeys "% ", True
Sends Alt+{space} to Word. This has the effect of opening the menu you
would open by clicking the icon in the top left corner of any application.
(I'm sure it has a name.)
What I left off was the next keystroke (and the thousand or so following).
The next keystroke is 'x', which maXimizes the application on the screen.

As to your other suggestions, thank you. I will look into automation, and
look up the references you have provided.

If the user has chosen centimeters, the app will probably not look good at
all.

I really hope to spend most of my future efforts creating a replacement app
instead of maintaining this one, but I must maintain pieces of it when them
breaking prevents the users from doing their work. About a dozen people use
the app all day long (Attorney's office) to maintain their cases.

Thanks for your help.

Wanna Be
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top