I am moving from task scheduler in XP to Windows7 and I am trying to understand it better.
I have a vbs script that is located in this location N:\Folder1\subFolder1\subFolder2\subFolder3\subFolder4\script.vbs
This script.vbs basically opens an excel file(.xlsm) in that same directory and runs a macro in that excel file and then closes the excel file.
The script gets the directory it is in using this code strPath = WshShell.CurrentDirectory.
The macro basically copies data from a csv file(located somewhere else) and may do some other data maniplation stuff.
I do this various amounts of times in various folders/subfolders like above, so basically there is one folder for every excel file, which would also have its own script.vbs file.
I then schedule these scripts to run in task scheduler.
In XP this was okay and worked in that the script was scheduled and it ran as requested.
But now I am on Windows7 and was hoping it would be a straight forward to schedule these again.
It turns out I have to put the location of every script.vbs in the Start in: (optional) parmaeter of the Action tab for each schedule.
But what I am wondering is, why I have to do this for every schedule? I thought I had this covered in getting the current Directory of the script.vbs in the script, or am I missing something. Any other comments/improvements welcome on this method.
Note:
I do not recall having to do this in windows XP but maybe windows XP done this for you.
I do not have to run with highest privaleges which is good as my password changed every 3 mths
'need to update WBName & MacroName here as this is fairly generic
dim WshShell
set WshShell = CreateObject("Wscript.Shell")
dim strPath
strPath = WshShell.CurrentDirectory
'WScript.echo("For Debugging: strPath")
'WScript.echo(strPath)
'msgbox strPath
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application")
'myExcelWorker.Visible = True ' this makes excel visible
dim oWorkBook
dim WBName
WBName = "\ExcelWorkBook.xlsm" 'WB to be opened
dim MacroName
MacroName = "'" & strpath & WBName & "'!macroName" 'Macro Name to be run
'Write Start+strPath to log file
Call WriteLog("Start_XXX",strPath,"var3")
'Write Mid+strPath+WBName to log file
Call WriteLog("Mid___XXX",strpath & WBName,"var3")
'open WB for running macro
'set oWorkBook = myExcelWorker.Workbooks.open(strpath & WBName) 'for WB WITHOUT password
Set oWorkBook = myExcelWorker.Workbooks.Open(strpath & WBName,,,,"","Password") 'for WB with password
'Write MacroName to log file
Call WriteLog("Mid___XXX",MacroName,"var3")
myExcelWorker.Run MacroName
myExcelWorker.DisplayAlerts = False 'this is required so the WB will save without being prompted
oWorkBook.Save
oWorkBook.Close
myExcelWorker.DisplayAlerts = True ' set it back to true again as it is good practice
myExcelWorker.Quit
'Write End to log file
Call WriteLog("End___XXX","t2","t3")
set oWorkBook = Nothing
set myExcelWorker = Nothing
set WshShell = Nothing
'sub to write to log file
Sub WriteLog(var1, var2, var3)
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
'Wscript.Echo "VBSStart.vbs is running"
Dim ObjFso
Dim StrFileName
Dim ObjFile
Dim FlName
'WScript.Echo var1 & ",,,," & var2
FlName = "TestFile.txt"
StrFileName = objShell.CurrentDirectory & "\" & FlName
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Creating a file for writing data
set ObjFile = ObjFso.OpenTextFile(StrFileName, 8, True)
'Writing a string into the file
ObjFile.WriteLine(var1 & "," & var2 & "," & var3 & "," & now)
'Closing the file
ObjFile.Close
'msgbox "near the end..."
' Using Set is mandatory
Set objShell = Nothing
End Sub