Solved

Why use of Application.SendKeys is not good coding practice in VBA, excel 2010

Posted on 2014-03-03
4
3,933 Views
Last Modified: 2014-03-04
Hi Team,
I came to know the use of Application.SendKeys is not a good coding practice better to use windows API to achieve this functionality, I want to know why it is not good coding practice and its impact on the system. I appreciate if you could provide in support some link from microsoft/Authentic to justify the same.
Hopefully looking forward to hearing from you.
Regards,
0
Comment
Question by:satmisha
  • 3
4 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39902509
SendKeys is the most crude way to communicate with other applications. The technique dates back to the first graphical user interface applications and before better ways of communicating between applications was developed. It's undependable and there are no ways to guarantee that the target objects are in place to receive the generated keystrokes.

The first better method to automate other applications was DDE or Dynamic Data Exchange:

http://en.wikipedia.org/wiki/Dynamic_Data_Exchange

These days it's much more common and preferable to use OLE or Object Linking and Embedding:

http://en.wikipedia.org/wiki/Object_Linking_and_Embedding

Only in extreme cases and when DDE/OLE are unavailable do we use SendKeys.

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39902512
Here are some examples of using SendKeys to do things in Excel that can't be done with the Excel object model:

http://blog.contextures.com/archives/2013/01/29/problems-with-sendkeys-in-excel/

Kevin
0
 

Author Comment

by:satmisha
ID: 39904462
Thanks Sir for your prompt reply.

But still seems my question is undone, may be I haven't mentioned it clearly, let me rephrase these:

1. Why Application.SendKeys is considered as bad practice and its impact on system.
2. Is there a better way to use replace this.
3. I appreciate if you could provide any supporting document for this.

Hopefully looking forward to hearing from you.
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 145 total points
ID: 39904930
> Why Application.SendKeys is considered as bad practice and its impact on system.

As I stated above, SendKeys is the most crude way to communicate with other applications. The technique dates back to the first graphical user interface applications and before better ways of communicating between applications was developed. It's undependable and there are no ways to guarantee that the target objects are in place to receive the generated keystrokes.

As far as impact on the system, there is virtually no impact on the system in terms of resource utilization. Not more than any other technique anyway.

> Is there a better way to use replace this.

As I stated above, there are two more acceptable and robust methods: DDE and OLE. See my first post above for details and links to extensive descriptions of these technologies.

Kevin
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question