Tip Send Keys Statement is Case Sensitive

When you send keystrokes with the SendKeys statement, bear in mind that you must distinguish between the lower- and uppercase characters. Therefore, to send the key combination Ctrl+d, you must use Ad, and to send Ctrl+Shift+D, you should use the following string: A+d.

Earlier in this chapter you learned that .cpl files launch various Control Panel icons. The purpose of the VBA procedure that you will now create is to locate on your hard drive all of the files with the .cpl extension.

1. Use the Immediate window to launch Windows Explorer with the following statement: Shell "Explorer." The "My Documents" icon should appear on the Windows taskbar at the bottom of the screen.

2. Insert a new module into the current project and rename it SendKeysStatement.

3. Enter the FindCPLFiles procedure, as shown below:

Sub FindCPLFiles()

' The keystrokes are for Windows 2000 AppActivate "My Documents" ' activate the Search window SendKeys "{F3}", True ' move the pointer to the Search for files ' and folders named text box SendKeys "%m", True ' type in the search string SendKeys "*.cpl", True ' move to the Look in drop down box SendKeys "{Tab}{Tab}", True ' change to the root directory SendKeys "C:\", True ' execute the Search SendKeys "%s", True End Sub

4. Switch to the Microsoft Excel application window and run the FindCPLFiles procedure (use Alt+F8 to open the Macro dialog, highlight the name of the procedure, and then click Run).

The first statement in the procedure above uses the AppActivate statement (see the preceding section) to activate the already running application. Recall that you activated the Windows Explorer with the Shell statement typed in the Immediate window. The remaining statements send necessary keystrokes to the active application. The result of this procedure is the Search Results window with a list of Control Panel files with the extension .cpl. You could use one SendKeys statement to send all of the required keystrokes (see the following example procedure). However, it's much easier to understand the procedure when keystrokes are sent in small batches.

Sub FindCPLFiles2()

AppActivate "My Documents" SendKeys "{F3}% m*.cpl{Tab}{Tab}C:\%s", True End Sub

0 0

Post a comment