AppActivate stopped working

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,345
I have the following code in a macro which used to work:

AppActivate "Microsoft Word"
SendKeys ("^v"), Wait:=True
SendKeys "{RIGHT 3}", Wait:=True
AppActivate "Microsoft Excel"

It has now decided to stop at the first of these lines with a Run-time error 5. Invalid procedure call or argument.

If I comment out the line, manually change focus to the already open Word window and continue the macro, it happily deals with the last line and returns focus back to Excel.

A couple of days ago this code was working for me and I can't think what may have made the difference or why it is failing me now
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
There are a few things that may be effecting this, first, check to make sure that Microsoft Word is still the wording on the bar at the top of the application. Second, make sure that the application is not minimized, the appactivate, at least for me does not seem to activate the program in the code, however it normally does not give a run time error either so this probably is not your problem. Last thing to check would be that with the appactivate the program must be open. If your Microsoft word program is not open it will give you the Run time error 5 message. If you are looking to activate a new Microsoft word program with your code, try the following:

Code:
Sub mycode()
Dim wurd
wurd= Shell("winword.exe", 1)
SendKeys ("^v"), Wait:=True
SendKeys "{RIGHT 3}", Wait:=True
AppActivate "Microsoft Excel"
End Sub

Best of luck!
 
Upvote 0
Honestly, I would recommend not using either SendKeys or AppActivate - as you seem to have found out they can just cause problems.:)

What are you actually trying to do?
 
Upvote 0
Thanks for that bjurney. I get a pass on all of your possible suggestions. Thanks also for the code snippet which I shall copy to my macro file.

I've had some success replacing the first of the lines quoted with

Word.Application.Activate

I've left the AppAcitivate "Microsofr Excel" as that has not yet given me any trouble.

All the best,

GB
 
Upvote 0
Thanks Norrie,

I hate send keys and have now learned not to trust AppActivate either.

What I do is run a long macro which generates a formated screen of data. I then use this macro to make a camera copy of the data, switch to Word and paste it into the document, then return to Excel.

I have extended the code to call another macro which automatically generates the next screen of data and I have been trying to make a loop to run through all of the data, up to 100 images in total.

What I am finding is that it mostly works, but sometimes in place of images in the Word document, I see the occasional "v" where SendKeys has failed.

If you can suggest a better way, I'll go for it.
 
Upvote 0

Forum statistics

Threads
1,216,087
Messages
6,128,740
Members
449,466
Latest member
Peter Juhnke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top