PDA

View Full Version : Excel VBA to add/delete windows scheduled task



khaledocom
01-22-2011, 09:45 PM
Hi all,

I'm using windows XP and Ms office 2003, I do need an urgent help to
add a scheduled task using Excel VBA to run .xls file for one month.
Then I need a code in Excel VBA to delete the same scheduled task after one month.

Please help.

Thanks in advance.

mdmackillop
01-23-2011, 06:43 AM
Check out OnTime in VBA Help, or is there more going on?

Kenneth Hobs
01-23-2011, 07:32 AM
There are two methods to use schtasks. One is by its API and the other is by the schtasks.exe. For the latter, put it and the command line parameters string in a Shell() command. For the former, that can be rather involved.

If you use the latter, try putting the string for the Shell() command into Start > Run, to make sure that the string is correct. The usual problem is that a string with space characters was not enclosed with quotes. You can also test using that string in a BAT file. Once you have the string built properly, you can build the string in VBA for Shell().

The best source for both methods is: http://msdn.microsoft.com/en-us/library/aa383608

There are various other API examples.
1. http://www.tek-tips.com/viewthread.cfm?qid=356060
2. http://www.andreavb.com/forum/viewtopic_1507.html

khaledocom
02-08-2011, 01:00 AM
Thanks for your help, but still couldn't get it.
Please see below code and tell me what's wrong with it as WHS:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")errJobCreated = objNewJob.Create _
("C:\222\test.xls", "********073000.000000-420", _
True , 1 Or 2 Or 4 Or 8 Or 16, 32 ,64 , JobID)
Wscript.Echo errJobCreated


Brothers, again I'm very thankful for your help.

Kenneth Hobs
02-08-2011, 04:40 PM
Cross posted to http://www.mrexcel.com/forum/showthread.php?p=2605612