Click here to Skip to main content
14,975,411 members
Articles / Programming Languages / Visual Basic
Posted 5 Nov 2004


25 bookmarked

How to run Scheduled Task Wizard programmatically

Rate me:
Please Sign up or sign in to vote.
4.56/5 (10 votes)
5 Nov 2004CPOL2 min read
Demonstration of how to run Scheduled Task Wizard programmatically, in VB and Borland C++ Builder.


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.

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:

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
If Found = False Then
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

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

Image 1

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:

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);
     ShowMessage("Impossible! There are no any Scheduled Tasks folder");
//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)).
                int count2=vb.Exec(PropertyGet("Count"));
                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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vladimir Afanasyev
Web Developer
Ukraine Ukraine
I am C++ Builder developer.
I am interesting in WMI, Shell, some deep
COM interface. Beside these I am Brainbench
Win32 API Master.
Now I very like Microsoft .NET and C#. I made some firsts OPOS drivers for Ukrainian fiscal printers.

Comments and Discussions

GeneralAutomatic Maling Pin
am.net4-Mar-09 0:42
Memberam.net4-Mar-09 0:42 
hi to all....
i want to create web application in asp.net1.1, which can send mail on every morning at 10.00 am.

pls help me... thnx in advance.

i like to play with programming.... Smile | :)

GeneralASP.NET code to run existing windows scheduled task Pin
Jay_Gateway15-Feb-09 19:57
MemberJay_Gateway15-Feb-09 19:57 
GeneralThis scripts fails on WinXP SP1 (64bits) Pin
osvaldoc17-Jun-08 11:06
Memberosvaldoc17-Jun-08 11:06 
GeneralSheduled Tasks Initialize Pin
Andre Dias10-Mar-06 5:44
MemberAndre Dias10-Mar-06 5:44 
GeneralSheduled Tasks Pin
Andre Dias8-Mar-06 7:57
MemberAndre Dias8-Mar-06 7:57 
AnswerRe: Sheduled Tasks Pin
Vladimir Afanasyev9-Mar-06 4:37
MemberVladimir Afanasyev9-Mar-06 4:37 
GeneralRe: Sheduled Tasks Pin
Andre Dias9-Mar-06 7:57
MemberAndre Dias9-Mar-06 7:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.