Mar 30 2007
This is a utility I wrote for a department at work to stamp a date and time for an annotation in an e-mail. The e-mail client we are using does not support this feature so we have to do it manually.
The program is interesting because it shows how to send keystrokes from a C# application to another program. I was trying to make the target application active to send the keys, but the command to make it’s document active requires the document name in it’s arguments (see below). I found it simpler to set the DateStamp program so it’s always the topmost window and when you click the Stamp button, it will send an Alt-Tab keystroke to switch to the previous application. The date and time are then sent as key presses. It’s not a very elegant solution but it works.
You can make an application active and send commands with the following statements:
Microsoft.VisualBasic.Interaction.AppActivate("Untitled - Notepad");
SendKeys.Send (“This is a test.”);
If you are using C#, you need to add a reference to Microsoft.VisualBasic in order to use the above AppActivate function.
The Send command sends the text as key presses to the application. You can also use the SendWait function which does the same thing but also waits for the message to be processed. If the commands from SendWait are not being processed properly, a slight time delay may need to be set between commands. This is a problem with the .NET Framework.