Create Windows Task Scheduler entry using VBA

Create Windows Task Scheduler entry using VBA


Create Windows Task Scheduler entry using VBA

आप निम्नलिखित VBA code का प्रयोग करके Windows Task Scheduler में entry कर सकते हैं और किसी भी program / application को automate कर सकते हैं। 

Step 1Excel में "Visual Basic Editor" को open करें और एक नए module को insert करें। 

Windows Task Scheduler entry using VBA

Step 2 -  अब नीचे दिए गए code को Copy & paste करने के बाद procedure को run करें 
(Note : code में path (rpathname), date (SetDate) और time (SetTime) को अपने अनुसार change कर लें )

Windows Task Scheduler entry using VBA

Step 3 - आपको निम्न confirmation आएगा कि task सफलतापूर्वक हो गया है। 

Windows Task confirmation

Step 4 - आप Windows Task Scheduler में देख सकते हैं कि आपके username folder में कल के लिए एक task create हो गया है। 

Windows Task Scheduler

Option Explicit
Sub Script_MachineScheduling()
'This procedure will create entry for tomorrow with same time in Windows Task Scheduler and open the test.xlsm file.
'define variables
Dim intFileNum As Integer
Dim wsh As Object
Dim SetDate As Date
Dim RobotName As String, user As String, rpathname As String, CompleteCommand As String
Dim sFileName As String, SetTime As String, sFileNameMRobot As String
Set wsh = VBA.CreateObject("WScript.Shell")
'robot name into Windows Task Scheduler
RobotName = "Robot_" & Format(Now(), "DDMMYYYY_hhmmss")
'file that will be called using Windows Task Scheduler
rpathname = "C:\Users\username\Downloads\test.xlsm"  '<< change this path to yours
SetTime = Format(Now(), "hh:mm")            '<< change the time according to your future schedule
SetDate = Format(Now() + 1, "mm/dd/yyyy")   '<< change the date according to your future schedule
user = Environ$("Username")
    'If user name is blank or not found then use below function to get it.
    If user = "" Then
        user = Mid(WorksheetFunction.Substitute((Environ$("UserProfile")), "\", "_", 2), WorksheetFunction.Find("_", WorksheetFunction.Substitute((Environ$("UserProfile")), "\", "_", 2)) + 1, 8)
    End If
user = user & "\"
'command for creating task schedule
CompleteCommand = "SchTasks /Create /SC ONCE /TN " & user & RobotName & _
            " /TR " & """" & "\" & """" & rpathname & """" & """" & " /SD " & SetDate & " /ST " & SetTime & " /f"
CompleteCommand = CompleteCommand & vbCrLf & vbCrLf & "timeout /t 10 /nobreak"
' Write code for Windows Task Scheduler entry into bat file on disk
    sFileNameMRobot = "Windows_Task_Schedule_" & Format(Now(), "DDMMYYYY_hhmmss")
    sFileName = ThisWorkbook.Path & "\" & sFileNameMRobot & ".bat"
    
    intFileNum = FreeFile
    Open sFileName For Output As intFileNum
    Print #intFileNum, CompleteCommand
    Close intFileNum
'create entry into Windows Task Scheduler using bat file
Call Shell(ThisWorkbook.Path & "\" & sFileNameMRobot & ".bat", vbNormalFocus)
Set wsh = Nothing
End Sub

Script_MachineScheduling नामक यह VBA code, automatic रूप से एक Windows Task Scheduler entry बनाने के लिए design किया गया है जो कल उसी समय एक Excel file (test.xlsm) खोलती है। संक्षेप में, यह Excel को भविष्य में फिर से automatic रूप से चलने के लिए schedule करने में मदद करता है। आइए इसे सरल शब्दों में step by step समझते हैं।

Step 1: Setting up the macro

Option Explicit
Sub Script_MachineScheduling()

Option Explicit statement यह सुनिश्चित करता है कि use से पहले सभी variables declare किए जाने चाहिए - इससे variable names में typing की गलतियों से बचने में मदद मिलती है।
Macro का नाम, Script_MachineScheduling, इसके उद्देश्य का वर्णन करता है: यह machine पर किसी script या task को schedule करता है।

Step 2: Declaring variables

Dim intFileNum As Integer
Dim wsh As Object
Dim SetDate As Date
Dim RobotName As String, user As String, rpathname As String, CompleteCommand As String
Dim sFileName As String, SetTime As String, sFileNameMRobot As String

ये variable विभिन्न प्रकार के data store करते हैं:

  • intFileNum — batch (.bat) file में लिखने के लिए उपयोग किया जाता है।
  • wsh — एक Windows Script Host (WScript) object जिसका उपयोग system-level command execute करने के लिए किया जाता है।
  • SetDate और SetTime — scheduling के लिए date और time store करते हैं।
  • RobotName — schedule किए गए कार्य के लिए unique name।
  • user — वर्तमान Windows username store करता है।
  • rpathname — खोलने के लिए Excel file का पूरा path store करता है।
  • CompleteCommand, sFileName, sFileNameMRobot — batch file के लिए उपयोग की जाने वाली पूरी command string और file name store करते हैं।

Step 3: Creating the WScript Shell object

Set wsh = VBA.CreateObject("WScript.Shell")

यह line एक object बनाती है जो VBA को Windows के साथ सीधे communicate करने की अनुमति देता है। इस object के माध्यम से, VBA Command Prompt की तरह ही commands execute कर सकता है।

Step 4: Preparing task details

RobotName = "Robot_" & Format(Now(), "DDMMYYYY_hhmmss")
rpathname = "C:\Users\username\Downloads\test.xlsm"
SetTime = Format(Now(), "hh:mm")
SetDate = Format(Now() + 1, "mm/dd/yyyy")

  • Current date और time का उपयोग करके schedule किए गए कार्य के लिए एक unique name बनाया जाता है।
  • Excel file path निर्धारित किया जाता है (आपको इसे अपने actual file path से बदलना चाहिए)।
  • Time, current system time पर set किया जाता है।
  • Date कल (current date + 1) पर set किया जाता है।

Step 5: Finding the current Windows username

user = Environ$("Username")
If user = "" Then
   user = Mid(WorksheetFunction.Substitute((Environ$("UserProfile")), "\", "_", 2), WorksheetFunction.Find("_", WorksheetFunction.Substitute((Environ$("UserProfile")), "\", "_", 2)) + 1, 8)
End If
user = user & "\"

  • Environ$("Username") function Windows से logged-in किया गया username प्राप्त करता है।
  • यदि यह उसे नहीं ढूँढ पाता है, तो एक alternate method Windows username profile path से users का नाम निकालती है।
  • अंत में, formatting purposes के लिए username में एक backslash (\) जोड़ा जाता है।

Step 6: Creating the Windows Task Scheduler command

CompleteCommand = "SchTasks /Create /SC ONCE /TN " & user & RobotName & _
    " /TR " & """" & "\" & """" & rpathname & """" & """" & " /SD " & SetDate & " /ST " & SetTime & " /f"
CompleteCommand = CompleteCommand & vbCrLf & vbCrLf & "timeout /t 10 /nobreak"

  • यह command SchTasks का उपयोग करता है, जो schedule किए गए कार्यों को बनाने के लिए एक Windows utility का use करता है।
  • /SC ONCE — इसे केवल एक बार schedule करें।
  • /TN — username और robot name का उपयोग करके task का नाम set करता है।
  • /TR — यह define करता है कि कौन सा program या file चलानी है (इस मामले में Excel file)।
  • /SD और /ST — scheduled date और time define करते हैं।
  • /f — यदि कोई similar task मौजूद हो, तो भी task creation को force करता है।
  • Second line अचानक बंद होने से रोकने के लिए 10 second का timeout जोड़ती है।

Step 7: Writing the command into a batch file

sFileNameMRobot = "Windows_Task_Schedule_" & Format(Now(), "DDMMYYYY_hhmmss")
sFileName = ThisWorkbook.Path & "\" & sFileNameMRobot & ".bat"
intFileNum = FreeFile
Open sFileName For Output As intFileNum
Print #intFileNum, CompleteCommand
Close intFileNum

  • Current time का उपयोग करके एक unique batch file (.bat) नाम बनाया जाता है।
  • File को workbook वाले folder में save किया जाता है।
  • Code file को लिखने के लिए खोलता है और उसके अंदर पूरा command save कर देता है।
  • फिर batch file बंद कर दी जाती है।

Step 8: Executing the batch file

Call Shell(ThisWorkbook.Path & "\" & sFileNameMRobot & ".bat", vbNormalFocus)

यह line आपके द्वारा अभी-अभी बनाई गई batch file को चलाती है, जो बदले में Windows Task Scheduler में नए task को register करती है।
vbNormalFocus का अर्थ है कि batch file एक normal command window के साथ visible रूप से चलती है।

Step 9: Cleaning up

Set wsh = Nothing
End Sub

यह execute पूरा होने के बाद WScript object को memory से release कर देता है।

एक टिप्पणी भेजें

Please do not enter any spam link in the comment box.

और नया पुराने