Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to run Scheduled Task Wizard programmatically

0.00/5 (No votes)
5 Nov 2004 3  
Demonstration of how to run Scheduled Task Wizard programmatically, in VB and Borland C++ Builder.

Introduction

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.

Objectives

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.

Visual Basic

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 NameSpace of Shell object:

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
       Exit For
    End If
Next
If Found = False Then
    End
End If

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
       SchedWizard.InvokeVerb (Verbs.Item(0))
       Exit For
    End If
Next

When we run it, in the result, Scheduled Tasks Wizard also will be run:

Borland C++Builder

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:

CoInitialize(NULL);
Variant SA=Variant::CreateObject("Shell.Application");
Function NameSpace("NameSpace");
Function Items("Items");
//Virtual folder containing icons for the Control Panel 

//applications. (value = 3) 

int ssfCONTROLS=3;
//get it folder - scheduler there

Variant fold=SA.Exec(NameSpace << ssfCONTROLS);
//will be enum items of folder and search scheduler

Variant It=fold.Exec(Items);
int count=It.Exec(PropertyGet("Count"));
//Search folder with name "Scheduled Tasks" inside CPAs

Variant schedulerFold=Variant::Empty();

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);
           break;
        }
}
if(schedulerFold.IsEmpty())
{
     ShowMessage("Impossible! There are no any Scheduled Tasks folder");
     Application->Terminate();
     return;
}
//get FolderItem object:

 bool d=schedulerFold.Exec(PropertyGet("IsFolder"));
 Variant op=schedulerFold.Exec(PropertyGet("GetFolder"));
//find and run item "Add Scheduled Task"

//really find ver inside 

//and it is not big secret that verb called "Open"

 Variant It1=op.Exec(Items);
int count1=It1.Exec(PropertyGet("Count"));
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)).
            Exec(Function("Verbs"));
                int count2=vb.Exec(PropertyGet("Count"));
                AnsiString MyVerb=Variant(vb.Exec(Function("Item") << 0)).
                Exec(PropertyGet("Name"));
           Variant(It1.Exec(Function("Item") << i)).
            Exec(Function("InvokeVerb") << vb);
           break;
        }
}
CoUninitialize();

Conclusion

It was only a small demonstration of Shell Scriptable Objects. Really they have many more possibilities than I showed here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here