Executive Privilege


The Shell function is fine for normal use, but you can get better control of your programs with a CExecutive object. CExecutive does directly what the Shell function does behind the scenes, but instead of hard-coding defaults whether or not you want them, it puts you in control by letting you set lots of optional properties.


The base functionality of CExecutive is similar to Shell. The simplest command line looks like this:

Dim exec As New CExecutive
exec.Run "Notepad"

At first glance, CExecutive doesn’t seem to be bringing much to the party. But it starts to make a little more sense when you set the WaitMode and Show ­properties:

With exec
.WaitMode = ewmWaitIdle
.Show = vbHide
.Run "mktyplib shelllnk.odl"
If .ExitCode Then MsgBox "Compile failed"
End With

You don’t have to save the process ID and exit code in variables or call a separate WaitOnProgram because all that is built into the class. And there’s more. For Windows programs, you can request a position for the new window as in this example (which assumes you’ve set a With block):

' Notepad half the screen size 20 percent in from left and top
.Left = Screen.Width / Screen.TwipsPerPixelX * 0.2
.Top = Screen.Height / Screen.TwipsPerPixelY * 0.2
.Width = Screen.Width / Screen.TwipsPerPixelX * 0.5
.Height = Screen.Height / Screen.TwipsPerPixelY * 0.5
.Show = vbNormalFocus
.InitDir = "C:\"
.WaitMode = ewmNoWait
.Run "Notepad colors.txt"

Of course, a requested window position and size is just a request. The program itself can override anything the caller asks for. Calculator, for example, determines its own size and will ignore your requests to the contrary.