Command 'Copy' not available now

M

Matthew Pfluger

Hello all,

I've got a curious case. When I step through the following code, it works
just fine. When I let it run by itself, it throws the error 2046 - The
command or action 'Copy' isn't available now.

Here's the code:

===============================================
' Get screen information
Dim alngTwipsPerPixel(0 To 1) As Long
GetTwipsPerPixel alngTwipsPerPixel(0), alngTwipsPerPixel(1) ' returns 15,15

' Move mouse cursor to "Select All Records" button at top left corner of
datasheet
MoveMouse 25, frmSource.FormHeader.Height / alngTwipsPerPixel(0) + 25

' Send a command to left click the mouse
LeftMouseClick

' Copy all records, formatting, and captions to the Clipboard
DoCmd.RunCommand acCmdCopy ' code will fail here when running w/o
breakpoint

===============================================

The code is designed to click on the "Select All Records" button on the
datasheet portion of a Split Form (see my post "Use VBA to switch view in
Split Form" from yesterday) and copy all records to the Clipboard for pasting
into Excel. The reason I do this instead of accessing the Form's recordset
is that through this method you copy all formatting and field captions in
addition to displayed data.

Can anyone tell me why the code works when stepping through, but it fails
when running without debugging? Thank you for your help.

Matthew Pfluger
 
R

RonaldoOneNil

I suspect it is because the code runs quicker than the actions that it is
performing can take affect. So it is trying to copy before it has been
selected. In debug mode you are running slower so it has time to select stuff
before the copy.

Try putting a DoEvents before the copy or if that does not work then try an
On Error Resume so that it will keep trying the copy line until it succeeds.
Beware with this however because you could get into an infinite loop if an
error occurs somewhere else that can't be resolved.
 

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