This article shows you how to run Scheduled Task Wizard from your VB and Borland C++ Builder applications via Shell Objects for Scripting and Visual Basic.
Some days ago, I found the following question in a Borland’s conference:
"How do I programmatically fire up the Scheduled Task Wizard and/or the Scheduled Tasks folder? My app uses EXE parameters that fit in nicely with the task scheduler, but I don't believe the users read the help on how to use it--so I would like to create a menu item to drive the point home.”
I found a solution. It was interesting that solutions in VB and Borland C++ Builder can be similar.
You can use VBA or even Excel VB for it. It will work for both cases. To solve the problem, I decided to use Scriptable Shell Objects. These objects give you possibility to browse and move between directories, enumerate items (files and other directories), start applications, and even Control Panel Applications, and a lot other useful things. So I opened VB for Excel, added a new form, added a button on the form, and wrote a handler for this button.
Some declarations for variables:
Private Sub CommandButton1_Click()
Dim SA As Object
Dim vFolder, ItemFold, MyItemFolder, ShedulFold, SchedWizard, Verb As Object
Dim ShedulFoldX As Object
Now I created
Shell object (“
Shell.Application” is ProgramID):
Set SA = CreateObject("Shell.Application")
Well, if you open Control Panel, you find item with name “Scheduled Tasks”. Ah, it’s just what we need. Let us do programmatically the same that we do manually. Open Control Panel via method
ssfCONTROLS = 3
Set vFolder = SA.NameSpace(ssfCONTROLS)
Set ItemFold = vFolder.Items
Good, now let us enumerate all items in Control Panel till we find item with name – “Scheduled Tasks”. OK, you are right, it will have another name for native localized Windows (but I show now it for English-language Windows version. Be sure if you change name to native, it will work for native Windows):
Found = False
For Each MyItemFolder In ItemFold
If MyItemFolder.Name = "Scheduled Tasks" Then
Set ShedulFold = MyItemFolder.GetFolder
Found = True
If Found = False Then
And get collection of items in “Scheduled tasks” folder:
Set ShedulFoldX = ShedulFold.Items
And now the last step – we must find item “Add Scheduled Task”, enumerate Verbs (that can work with this item), and do the appropriate verb. It is not a secret that there exists only one verb for “Add Scheduled Task”, and it’s name is “Open”:
For Each SchedWizard In ShedulFoldX
If SchedWizard.Name = "Add Scheduled Task" Then
Set Verbs = SchedWizard.Verbs
When we run it, in the result, Scheduled Tasks Wizard also will be run:
Now you can see that code written in Borland C++ (using type
Variant and late binding -
IDispatch interface) really similar to code written in VB:
Variant fold=SA.Exec(NameSpace << ssfCONTROLS);
for(int i=0; i<count; ++i)
if(AnsiString(Variant(It.Exec(Function("Item") << i)).
Exec(PropertyGet("Name"))) == "Scheduled Tasks" )
schedulerFold=It.Exec(Function("Item") << i);
ShowMessage("Impossible! There are no any Scheduled Tasks folder");
for(int i=0; i<count1; ++i)
if(AnsiString(Variant(It1.Exec(Function("Item") << i)).
Exec(PropertyGet("Name"))) == "Add Scheduled Task" )
Variant vb=Variant(It1.Exec(Function("Item") << i)).
AnsiString MyVerb=Variant(vb.Exec(Function("Item") << 0)).
Variant(It1.Exec(Function("Item") << i)).
Exec(Function("InvokeVerb") << vb);
It was only a small demonstration of Shell Scriptable Objects. Really they have many more possibilities than I showed here.