DoEvents and SendKeys

J

Janie

I am running a utiltity (CutePDF) that allows me to output my report as a
PDF. Works great ... most of the time. When the dialog comes up, the user
must insert the report name and CutePDF saves it as a PDF file. Love it.
Here's the problem: I have a SendKeys that fills in the name of the report
and this works 90% of the time. The problem is I need to slow things down so
that the whole string gets entered before the action picks up again. I know
the DoEvents is exactly what I need to use. My problem is I am not quite
sure how to place it.

So, if I have something that is:
SendKeys mystring

where is it that I place the DoEvents???

thanks for any insight.

Janie
 
R

Robert Morley

You can't slow down SendKeys itself, but there are a couple of things you
may want to try.

Before you try anything to do with DoEvents, try using the optional "Wait"
parameter to SendKeys to tell it to wait for the keystrokes to finish before
proceeding. See if that addresses your problem. This is probably as simply
as changing your code to read:

SendKeys mystring, True

Second, whether or not you end up using DoEvents, it may also be helpful (if
it's appropriate) to split "mystring" into several parts, one for each
window that pops up.

Now on to DoEvents: the simplest thing to do is to put a single DoEvents
before, after, and between any SendKeys statements. If that doesn't work,
you can use timer loops similar to the following anywhere that they're
necessary:

Dim sngTimer As Single

sngTimer = Timer()
Do
DoEvents
Loop Until Timer() - sngTimer > 1

....where "1" is the number of seconds you want to wait (you can make it a
decimal number like 0.25 if you don't need to wait very long). You would
probably want to make a separate function out of the code above if you plan
to use it in more than one place.



Rob

You can try placing DoEvents both before and after the SendKeys for the
maximum effect. If you want, you
 
J

Janie

Yes, I forgot to indicate I do have the wait as TRUE. It still was giving
enough time for the string to finish writing. Since I only have to complete
one line, the multiple variables isn't exactly what I need.

I will give the DoEvents a try ... I am curoius ... you say write a DoEvents
before, after and between my SendKeys ... so does that mean just simply


DoEvents
SendKeys mystring, True
DoEvents
SendKeys "{enter}", Tru
DoEvents

Everything I have read has cautioned about having more than one DoEvents in
a procedure, so I wanted to make sure I understood you correctly.
 
R

Robert Morley

That's what I'm suggesting, yes.

There's nothing wrong with having more than one DoEvents in a procedure, as
long as you're aware of its potential repercussions. Even having just one
can lead to the possibility of multitasking within the same Access
application, or having windows pop up over where you're trying to do the
SendKeys, etc., but sometimes there's simply no help for it, or it's just
plain desirable to do so.

When you're forced to use this approach (or choose to do so), I strongly
recommend making sure that NOTHING touches your keyboard or mouse while the
SendKeys are happening, that you will NOT get unexpected popup windows
coming to the foreground, etc.

If you want to get more advanced, you can also use the Sleep API call. It
may or may not help you out in this case, you'd have to try it and see.
What it does is puts your Access app "on hold" for a short period of time,
without allowing other events to take place in Access. I'm not sure if it
would work out for the SendKeys that you're trying to do, but it might. If
you need more info on that, search the MS knowledge base, or post back to
the thread and I'll find you something on it tomorrow.



Rob
 
A

Albert D.Kallal

Don't use sendkeys.

While the sendkeys is being pressing, what happens if the user clicks on
their web browser, or clicks and moves to their ebay account..and then that
page starts receiving the keystrokes. They might just purchase a house they
did not want!!!

Remember, sendkeys is something we use as a last resort. The fact that find
it only works 90% of the time is a great indication.

For example, if you launch word..and start typing.....do the characters
appear? Well, usually we have to WAIT UNTIL word is finished loading..and
then you start typing. The problem here is that the computer does not know
to "wait" until the other dialog/program is finished loading BEFORE the
typing starts.

A much better solution is to dump the cutePDF. And use Stephens solution
here:

http://www.lebans.com/reporttopdf.htm

The above solution means several things

1) you don't have to install the pdf printer driver, as this solution
does not use one
2) you can provide the name of the output file in code, and not have to
resort to sendkeys..which, who knows what appcation running will receive the
keystrokes (a user bumping their mouse can change the focus..and now the
keystrokes are going to a different program!!).
3) Users will not see some dialog box flash by.

Check out the above example, as it solves many problems, and allow you to
specify the output file in code....
 

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